国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:23:43
字體:
來源:轉載
供稿:網友
由于項目需要, 需要對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);
}
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚松县| 陇川县| 邵阳县| 凉城县| 雷山县| 开封县| 阜新| 铁岭市| 安远县| 丘北县| 伊吾县| 台南县| 鹤壁市| 永新县| 临邑县| 汨罗市| 大城县| 苗栗县| 黄陵县| 双牌县| 黔西| 兰考县| 平远县| 冀州市| 丹棱县| 新河县| 巴林左旗| 清远市| 鹤庆县| 南投县| 湟中县| 息烽县| 垣曲县| 额尔古纳市| 农安县| 昔阳县| 江门市| 浏阳市| 五家渠市| 五河县| 嘉禾县|