关于DataGridView的颜色变化(着急) 好像是dataGrid.DisplayStyle.BackColor或者是dataGrid.Rows[i].DisplayStyle.BackColor,自己试试吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚写了一篇这方面的文章,不过是datagird的,不是dataGridview,2005支持datagrid,有兴趣你可以看看。http://blog.csdn.net/tjvictor/archive/2007/01/22/1489972.aspx 我想知道如何只把单元格的颜色变化。当一个DataGridView的第一行第三列的数据为"异常"时,此是只把第一行第三列的单元格的颜色变为红色,怎么实现呢?这个例子好实现吗?大家帮帮忙。 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if(e.RowIndex == 1 && e.ColumnIndex == 0) { e.CellStyle.BackColor = Color.Red; } } lz:到面前为止,net里的DataGridView控件不能实现你的要求,如下: private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 2 && e.RowIndex != -1) //判断所在列 { DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; int a = Convert.ToInt32(cell.Value); if (a > 10) //判断变色条件 { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood; //这里把整行的背景色都变了。 } } }想必上面的代码你很清楚,DataGridView控件无法实现你的需求,两个解决办法:1、自己扩展DataGridView控件,使之实现你的要求。2、找第三方控件。 这里我用过DevExpress的GridControl控件,功能非常强大,完全可以实现这个功能,我给出部分代码,你看看就明白了:(它可以直接设置每个单元格的外观)using DevExpress.XtraGrid.Views.Grid;// ...private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { GridView view = sender as GridView; if(e.RowHandle >= 0) { string category = view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]); if(category == "Beverages") { e.Appearance.BackColor = Color.Salmon; e.Appearance.BackColor2 = Color.SeaShell; } }} 不好意思,DataGridView能实现这个功能: private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 2 && e.RowIndex != -1) { DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; int a = Convert.ToInt32(cell.Value); if (a > 10) { e.CellStyle.BackColor = Color.BurlyWood; //dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood; } } }平时总是考虑DataGridViewCell ,把事件参数的信息给忽略了。 现在的DataGridView不比GridControl差了,而且扩展功能强以前2003也是用DataGrid扩展 现在让单元格的颜色变化,我实现了。这段代码我放在form_load里面。 for (int i = 0; i < dataGridView2.Rows.Count; i++) { if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("警告")) { dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Red; } else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("異常")) { dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Yellow; } else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("正常")) { dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Green; } }但是为什么我点击列头排序时颜色不随着上面的判断条件发生变化呢(上面的代码我放在dataGridView2_CellPainting),难道单元格的颜色第一次改了以后就不能改了吗? sdl2005lyx() 像是在做广告,呵呵! C#中向MySQL数据库存储图片的问题 C#实现《飞越星空》屏保——附带完整工程源码 创建文件时使用事务 求正则表达式 怎样实现javascript popup子窗口后 父窗口非活用 小弟没用过C#,谁能帮忙解释下面代码的,每句话实现了什么功能? 调用,,要这么实现 服务器接收字节数组发生丢失的问题 判断一串字符前缀0的个数 请问老手们:有关OA的 C#一个加密解密的方法 用C#编写获取远程IP,MAC的方法
http://blog.csdn.net/tjvictor/archive/2007/01/22/1489972.aspx
{
if(e.RowIndex == 1 && e.ColumnIndex == 0)
{
e.CellStyle.BackColor = Color.Red;
}
}
{
if (e.ColumnIndex == 2 && e.RowIndex != -1) //判断所在列
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
int a = Convert.ToInt32(cell.Value);
if (a > 10) //判断变色条件
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood; //这里把整行的背景色都变了。
} }
}想必上面的代码你很清楚,DataGridView控件无法实现你的需求,两个解决办法:
1、自己扩展DataGridView控件,使之实现你的要求。
2、找第三方控件。
这里我用过DevExpress的GridControl控件,功能非常强大,完全可以实现这个功能,我给出部分代码,你看看就明白了:(它可以直接设置每个单元格的外观)using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
GridView view = sender as GridView;
if(e.RowHandle >= 0) {
string category = view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]);
if(category == "Beverages") {
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
}
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2 && e.RowIndex != -1)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
int a = Convert.ToInt32(cell.Value);
if (a > 10)
{
e.CellStyle.BackColor = Color.BurlyWood;
//dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood;
} }
}平时总是考虑DataGridViewCell ,把事件参数的信息给忽略了。
以前2003也是用DataGrid扩展
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("警告"))
{
dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Red;
}
else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("異常"))
{
dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Yellow;
}
else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("正常"))
{
dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Green;
}
}但是为什么我点击列头排序时颜色不随着上面的判断条件发生变化呢(上面的代码我放在dataGridView2_CellPainting),难道单元格的颜色第一次改了以后就不能改了吗?