winform下如何改变dataGrid指定某行的样式(如颜色等)

hujiiori 2005-03-25 10:55:26
rt
...全文
337 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
syeerzy 2005-03-25
  • 打赏
  • 举报
回复
上面那么多代码,能用先用了吧,个人觉得没必要现在特别研究这个。

因为迟早要升到2005上去,新加的“主题”功能要是不能提供这种,那微软别混了。
tangyong12 2005-03-25
  • 打赏
  • 举报
回复
直接设置SectionBackColor和SectionForeColor属性.
LiloZhu 2005-03-25
  • 打赏
  • 举报
回复
datagrid 的樣式表(DataGridTableStyle)應用...
首先 我們先定一個 datatable 和 一個datarow

Private idtb_temp As New DataTable
Private idrw_row As DataRow

private sub GetDataTable()
idtb_temp.Columns.Add("prdodr_subodr_code") '''定義datatable 的列名

idtb_temp.TableName = "SearchTable"
Dim ldcl_header As Windows.Forms.DataGridTextBoxColumn
Dim ldgts_styles As New Windows.Forms.DataGridTableStyle
ldgts_styles.SelectionForeColor = System.Drawing.Color.Yellow
'''選中行的前景色,即字體顏色
ldgts_styles.SelectionBackColor = System.Drawing.Color.Brown '''選中行的背景色

ldgts_styles.ForeColor = System.Drawing.Color.Coral
''' datagrid 中將要顯示的字的顏色
ldgts_styles.AlternatingBackColor = System.Drawing.Color.Cyan
'''datagrid中奇數行所顯示的顏色
ldgts_styles.BackColor = System.Drawing.Color.Cyan
'''datagrid中偶數行所顯示的顏色

ldgts_styles.AllowSorting = False
'''些樣式表定義datagrid不允許自動排序..

ldgts_styles.MappingName = "SearchTable"

ldcl_header = New Windows.Forms.DataGridTextBoxColumn
'''實例化一個datagridtextboxcolumn
ldcl_header.MappingName = "prdodr_subodr_code"
'''引用前面定義的 “列名”
ldcl_header.HeaderText = "第一列"
'''datagrid 中顯示的 表列頭 文字
ldcl_header.ReadOnly = True '''些列設定為只讀
ldcl_header.TextBox.BorderStyle = BorderStyle.Fixed3D
ldcl_header.TextBox.ForeColor = System.Drawing.Color.Red

ldgts_styles.GridColumnStyles.Add(ldcl_header)

For i As Integer = 0 To 7
idrw_row = idtb_temp.NewRow
idrw_row.Item("prdodr_subodr_code") = "第" & i & "行"
idtb_temp.Rows.Add(idrw_row)

Next

idtb_temp.DefaultView.AllowNew = False
Me.DataGrid1.TableStyles.Add(ldgts_styles)
Me.DataGrid1.DataSource = idtb_temp
end sub

深山老翁 2005-03-25
  • 打赏
  • 举报
回复
呵呵,这个自己重绘了,设置datagrid某一行的背景色:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridCellFormatting
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 56);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(536, 296);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(560, 389);
this.Controls.Add(this.dataGrid1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion


private void FormatGridCells(object sender, DataGridFormatCellEventArgs e)
{
//color row 1 red
if(e.Row == 0)
e.BackBrush = Brushes.Red;
if(e.Row == 1)
e.BackBrush = Brushes.Red;
if(e.Row == 2)
e.BackBrush = Brushes.Red;
if(e.Row == 3)
e.BackBrush = Brushes.Red;

//color column 4 blue
// if(e.Column == 4)
// e.BackBrush = Brushes.Blue;
//
// //set font of some cells to bold
// if( (e.Row + e.Column) % 5 == 0 )
// e.TextFont = new Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold);
//
// //set textcolor of some cells to blue
// if( (e.Row + e.Column) % 8 == 0 )
// e.ForeBrush = Brushes.DodgerBlue;
//
// //set font of some cells to bold, underline, italic with white text on green background
// if( (e.Row + e.Column) % 9 == 0 )
// {
// e.TextFont = new Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
// e.ForeBrush = Brushes.White;
// e.BackBrush = Brushes.Green;
// }


}

private DataTable SomeDataTable()
{
DataTable dt = new DataTable("MyTable");

//add some columns
int nCols = 10;
for(int j = 0; j < nCols; ++j)
dt.Columns.Add(new DataColumn(string.Format("col{0}", j), typeof(string)));

//add some rows
int nRows = 40;
for(int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for(int j = 0; j < nCols; ++j)
dr[j] = string.Format("row {0} col {1}", i, j);
dt.Rows.Add(dr);
}

dt.DefaultView.AllowNew = false;//turn off append row
return dt;
}

private void AddCellFormattingColumnStyles(DataGrid grid, FormatCellEventHandler handler)
{
DataGridTableStyle ts = new DataGridTableStyle();

DataTable dt = (DataTable) grid.DataSource;

ts.MappingName = dt.TableName;

for(int j = 0; j < dt.Columns.Count; ++j)
{
DataGridFormattableTextBoxColumn cs = new DataGridFormattableTextBoxColumn(j);
cs.MappingName = dt.Columns[j].ColumnName;
cs.HeaderText = dt.Columns[j].ColumnName;
cs.SetCellFormat += handler;
ts.GridColumnStyles.Add(cs);
}

grid.TableStyles.Clear();
grid.TableStyles.Add(ts);

}
private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{

}

private void Form2_Load(object sender, System.EventArgs e)
{
this.dataGrid1.DataSource = SomeDataTable();

AddCellFormattingColumnStyles(this.dataGrid1, new FormatCellEventHandler(FormatGridCells));
}

public delegate void FormatCellEventHandler(object sender, DataGridFormatCellEventArgs e);

public class DataGridFormatCellEventArgs : EventArgs
{
private int _column;
private int _row;
private Font _font;
private Brush _backBrush;
private Brush _foreBrush;
private bool _useBaseClassDrawing;


public DataGridFormatCellEventArgs(int row, int col, Font font1, Brush backBrush, Brush foreBrush)
{
_row = row;
_column = col;
_font = font1;
_backBrush = backBrush;
_foreBrush = foreBrush;
_useBaseClassDrawing = false;
}

public int Column
{
get{ return _column;}
set{ _column = value;}
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public Font TextFont
{
get{ return _font;}
set{ _font = value;}
}

public Brush BackBrush
{
get{ return _backBrush;}
set{ _backBrush = value;}
}
public Brush ForeBrush
{
get{ return _foreBrush;}
set{ _foreBrush = value;}
}
public bool UseBaseClassDrawing
{
get{ return _useBaseClassDrawing;}
set{ _useBaseClassDrawing = value;}
}
}

public class DataGridFormattableTextBoxColumn : DataGridTextBoxColumn
{
//in your handler, set the EnableValue to true or false, depending upon the row & col
public event FormatCellEventHandler SetCellFormat;

private int _col;

public DataGridFormattableTextBoxColumn(int col)
{
_col = col;
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, this._col, this.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush);
if(SetCellFormat != null)
{
SetCellFormat(this, e);
}
if(e.UseBaseClassDrawing)
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
else
{
g.FillRectangle(e.BackBrush, bounds);
g.DrawString(this.GetColumnValueAtRow(source, rowNum).ToString(), e.TextFont, e.ForeBrush, bounds.X, bounds.Y);
}
if(e.TextFont != this.DataGridTableStyle.DataGrid.Font)
e.TextFont.Dispose();
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
//comment to make cells unable to become editable
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}

}
}
}


只需要改变FormatGridCells中的行就行了

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧