六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

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

[摘要]由于項目需要, 需要對DataGrid的數(shù)據(jù)行, 按不同的條件以不同的背景色相區(qū)別。 由于DataGrid中沒有相關(guān)的屬性和方法可以直接設(shè)置,要完成這個功能還挺費些功夫。在網(wǎng)上搜了半天,也沒找到解決...
由于項目需要, 需要對DataGrid的數(shù)據(jù)行, 按不同的條件以不同的背景色相區(qū)別。 由于DataGrid中沒有相關(guān)的屬性和方法可以直接設(shè)置,要完成這個功能還挺費些功夫。在網(wǎng)上搜了半天,也沒找到解決方案。只好自己動手,豐衣足食了,:) 。研究了半天, 終于搞定它了。好東西不敢獨享,特貼出來,希望能給需要的人帶來些幫助。



{

//...

//使用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;

//為每個單元格建立設(shè)置背景色的事件.
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)
{
//根據(jù)條件, 將相關(guān)行設(shè)置不同的背景色.
//下例為國家(datagrid中第9列)為Mexico的行設(shè)置為紅色,USA的行設(shè)為黃色.
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)
{
//重繪畫時,設(shè)置當(dāng)前行的背景色
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);
}
}



主站蜘蛛池模板: 日本亚洲天堂网 | 日本免费一区二区视频 | 天天做夜夜操 | 人禽交在线视频免费播放 | 日韩av线观看 | 亚洲成人xxx| 天天操天天射天天爽 | 小草影院在线观看 | 日本高清在线观看视频www | 日韩欧美中文字幕一区 | 午夜免费在线观看 | 午夜有码 | 亚洲精品综合在线 | 色综合桃花网 | 欧美午夜视频在线观看 | 五月婷婷之综合激情 | 日韩一级大片 | 青草视频免费 | 欧美一级三级在线观看 | 欧美亚洲激情视频 | 性欧美人与物videos另类 | 亚洲精品福利你懂 | 亚欧精品在线观看 | 色婷婷久久综合中文久久一本 | 中文字幕在线视频不卡 | 亚洲伦理在线观看 | 青青青青青青在线精品视频 | 特级黄一级播放 | 青春草在线观看播放免费视频 | 日韩美香港a一级毛片 | 香蕉国产综合久久猫咪 | 四虎成人4hutv影院 | 色综合天天射 | 婷婷五月在线视频 | 日本96在线精品视频免费观看 | 性色午夜视频免费男人的天堂 | 欧洲激情网站 | 五级床片全部免费播放 | 全国男人的天堂网站 | 污污免费在线观看 | 亚洲国产精品成人午夜在线观看 |