日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

將DataGrid中滿足條件的行設為不同的背景色(WinForm).

將DataGrid中滿足條件的行設為不同的背景色(WinForm).

更新時間:2022-05-16 文章作者:未知 信息來源:網絡 閱讀次數:

由于項目需要, 需要對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);
}
}

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 天天艹夜夜艹 | 欧美亚洲激情在线 | 精品国产一区二区三区19 | 在线欧美一级毛片免费观看 | 国内精品久久久久久久久野战 | 999精品久久久中文字幕蜜桃 | 91福利在线免费观看 | 亚洲 欧美 另类 天天更新影院 | 91国语精品自产拍在线观看性色 | 亚洲国产精品一区二区九九 | 欧乱色国产精品兔费视频 | 狠狠躁夜夜躁人人爽天天3 狠狠躁夜夜躁人人爽天天不 | 日本免费xxxx | 久久996re热这里只有精品 | 久草免费在线视频观看 | 99久久久国产精品免费牛牛四川 | 亚洲精品中文字幕一区在线 | 亚洲一区二区三区四区视频 | 美日韩一区二区三区 | 亚洲 欧美 国产 制服 动漫 | 蜜桃在线视频 | 午夜看一级特黄a大片黑 | 久久婷婷是五月综合色狠狠 | 欧美日韩视频在线第一区二区三区 | 亚洲天堂中文字幕在线观看 | 天天骑夜夜操 | 欧美日本三级 | 久久天天躁夜夜躁狠狠躁2020 | 中文字幕 一区 婷婷 在线 | 激情久久免费视频 | 伊人色综合久久 | 欧美做爰猛烈床戏大尺度排行 | 免费高清毛片在线播放视频 | 91黄瓜视频| 99这里精品 | 丰满美女福利视频在线播放 | 清纯美女校花在线啪视频播放器 | 后天在线观看 | 天天干影院 | 日韩视频大全 | 国产精品高清免费网站 |