将DataGrid中满足条件的行设为不同的背景色(WinForm).
2004-06-13 16:01
507 查看
由于项目需要, 需要对DataGrid的数据行, 按不同的条件以不同的背景色相区别。 由于DataGrid中没有相关的属性和方法可以直接设置,要完成这个功能还挺费些功夫。在网上搜了半天,也没找到解决方案。只好自己动手,丰衣足食了,:) 。研究了半天, 终于搞定它了。好东西不敢独享,特贴出来,希望能给需要的人带来些帮助。
{
//...
//使用DataGridTableStyle 显示DataGrid.
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "customers";
int numCols = _dataSet.Tables["customers"].Columns.Count;
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName;
columnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName;
//为每个单元格建立设置背景色的事件.
columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);
tableStyle.GridColumnStyles.Add(columnTextColumn);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = _dataSet.Tables["customers"];
}
public void SetColorValues(object sender, DataGridCellColorEventArgs e)
{
//根据条件, 将相关行设置不同的背景色.
//下例为国家(datagrid中第9列)为Mexico的行设置为红色,USA的行设为黄色.
if(Convert.ToString(dataGrid1[e.Row,8]) == "Mexico")
e.BackColor = Color.Red;
else if(Convert.ToString(dataGrid1[e.Row,8]) == "USA")
e.BackColor = Color.Yellow;
}
public class DataGridCellColorEventArgs : EventArgs
{
private int _row;
private Color _backcolor;
public DataGridCellColorEventArgs(int row, Color val)
{
_row = row;
_backcolor = val;
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public Color BackColor
{
get{ return _backcolor;}
set{ _backcolor = value;}
}
}
//为事件建立委托.
public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);
public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
{
public event CellColorEventHandler CheckCellColor;
public DataGridCellColorTextBoxColumn()
{
}
//继承DataGridTextBoxColumn的Pain事件.
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)
{
if(CheckCellColor != null)
{
//重绘画时,设置当前行的背景色
DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
CheckCellColor(this, e);
if(e.BackColor != Color.White)
backBrush = new SolidBrush(e.BackColor);
}
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
}
{
//...
//使用DataGridTableStyle 显示DataGrid.
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "customers";
int numCols = _dataSet.Tables["customers"].Columns.Count;
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName;
columnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName;
//为每个单元格建立设置背景色的事件.
columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);
tableStyle.GridColumnStyles.Add(columnTextColumn);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = _dataSet.Tables["customers"];
}
public void SetColorValues(object sender, DataGridCellColorEventArgs e)
{
//根据条件, 将相关行设置不同的背景色.
//下例为国家(datagrid中第9列)为Mexico的行设置为红色,USA的行设为黄色.
if(Convert.ToString(dataGrid1[e.Row,8]) == "Mexico")
e.BackColor = Color.Red;
else if(Convert.ToString(dataGrid1[e.Row,8]) == "USA")
e.BackColor = Color.Yellow;
}
public class DataGridCellColorEventArgs : EventArgs
{
private int _row;
private Color _backcolor;
public DataGridCellColorEventArgs(int row, Color val)
{
_row = row;
_backcolor = val;
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public Color BackColor
{
get{ return _backcolor;}
set{ _backcolor = value;}
}
}
//为事件建立委托.
public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);
public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
{
public event CellColorEventHandler CheckCellColor;
public DataGridCellColorTextBoxColumn()
{
}
//继承DataGridTextBoxColumn的Pain事件.
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)
{
if(CheckCellColor != null)
{
//重绘画时,设置当前行的背景色
DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
CheckCellColor(this, e);
if(e.BackColor != Color.White)
backBrush = new SolidBrush(e.BackColor);
}
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
}
相关文章推荐
- 将DataGrid中满足条件的行设为不同的背景色(winform)之二 (转载)
- 将DataGrid中满足条件的行设为不同的背景色(WinForm).
- 将DataGrid中满足条件的行设为不同的背景色(WinForm)
- 将DataGrid中满足条件的行设为不同的背景色(WinForm)
- 将DataGrid中满足条件的行设为不同的背景色(WinForm). (转载)
- 如何使DataGrid(Web)的不同行根据不同的条件有不同的显示格式?
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- 根据不同条件设置ITEM的背景色和鼠标移动时的背景色
- DataGrid表格实现根据不同条件对不同列数据进行编辑
- Datagrid数据绑定后,根据不同条件判断显示问题
- 如何使满足条件的数据显示不同的颜色
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- 给DataGrid中满足条件的超链接列加颜色
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法(转)
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- Winform DataGrid设计三步曲之三----如何改变DataGrid的背景色
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- 在DataGrid(WinForm)中改变符合指定条件的行颜色的方法
- 已知直角三角形的斜边是某个整数,并且要求另外两条边也必须是整数。求满足这个条件的不同直角三角形的个数。