将DataGrid中满足条件的行设为不同的背景色(WinForm)
来源:互联网 发布:淘宝流量会影响排名吗 编辑:程序博客网 时间:2024/05/02 11:41
{
//...
//使用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)
- Flex中如何将编译后的swf背景色设为透明
- 将空间设为背景色
- 如何更改DataGrid中某一行的背景色
- CStatic控件的背景色设为透明?
- 改变Flex DataGrid 行的背景色
- 改变Flex DataGrid 行的背景色
- EasyUI datagrid 改变行的背景色
- 根据不同条件设置ITEM的背景色和鼠标移动时的背景色
- 为文本的不同部分添加不同的背景色
- 改变datagrid的背景色
- 将文档中className有“red”的标签背景色设为红色(百度2014校招前端笔试题)
- Winform DataGrid设计三步曲之三----如何改变DataGrid的背景色
- C#的winform中MDI 父窗体改变背景色
- css中的@media only screen and ( ){}代码用来判断屏幕:当满足()中的条件时执行{}中的代码。 比如下面的代码:当屏幕宽度小于800px时内容区背景色为红色。 css中
- 将位图的背景色置为透明
- 给DataGrid中满足条件的超链接列加颜色
- 不用数据源直接连sqlserver数据库(jsp,中文)
- WINDOWS 2000 环境下JAVA环境配置
- 安一个blog家。
- 这个blog功能还算不错,还可以隐藏一些属于自己的东西。
- ' Btn_clear_Click(Nothing, Nothing)
- 将DataGrid中满足条件的行设为不同的背景色(WinForm)
- 纯代码方式的图片透明传输
- 我的blog也开通了
- 成功销售人员的“基因”
- test
- hoge66的专栏
- 用预编译好的rpm包来生成一个比较小的linux
- about serial
- My Big Big Blog