请教一下如何设置GridView中特殊行的背景色。 将GridView1绑定到一个DataTable1,DataTable1的结构如下:产品名称 产量 是否合格A 30 0B 20 1C 39 0D 40 1现在需要将是否合格列中值为0的行的背景色设为红色,该怎么做呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先循环查找嵌入DATAGRIDVIEW的数据然后得到索引再根据索引设置cell的背景色或者去看datagridview 有没有条件设置的属性 还可以在CellFormating事件里处理 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if(e.RowIndex == 0) { e.CellStyle.BackColor = Color.Blue; } else if(e.RowIndex == 1) { e.CellStyle.BackColor = Color.Red; } else { e.CellStyle.BackColor = Color.Green; } }你可以根据你的条件判断哪行需要改变颜色,根据上边颜色设置下。 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow) { string _value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "列的名字")); if (_value == "0") { e.Row.BackColor = System.Drawing.Color.Red; } }} 在GridView的RowDataBound事件中,如果"是否合格"在第三列,代码如下: if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToInt32(e.Row.Cells[2].Text) == 0) e.Row.BackColor = System.Drawing.Color.Red; } 你可以在dataGridView绑定后判断假如你上面的表结构绑定dataGridView后,是否合格是最后一列的话可以:for(int i=0;i<dataGridView.Columns.Count;i++){ if(i!=2);//如果当前列不是【是否合格】这一列則不再进行下面的比较 continue; for(int j=0;j<dataGridView.RowCount;j++) { if(dataGridView.Rows[j].Cells[i].FormatValue.ToString()=="0") { dataGridView.Rows[j].Cells[i].CellStyle.BackColor = Color.你想要的颜色; } }} C#项目断点调试com接口失败 符号未加载 求一条sql查询语句 这样的软件安全方案如何编写? DataGrid 绑定 ArrayList 时设置样式问题? 怎样让Button接受键盘的输入? 水晶报表 怎么用未邦定的字段 c#[图形应用] GetHbitmap性能问题 Gdi 高手进!!!! 本人已经熟悉VB、C、C++,今想转向C#,翻了翻《C#入门经典》和《C#高级编程》,觉得书太厚,篇幅太长,因此不得要领,请诸位根据我的情况 很不理解!!!!Web Services 到底是什么 请教几个简单的语法 code smith使用问题 vs2005中的烦人问题,高手进来指导~~
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.RowIndex == 0)
{
e.CellStyle.BackColor = Color.Blue;
}
else if(e.RowIndex == 1)
{
e.CellStyle.BackColor = Color.Red;
}
else
{
e.CellStyle.BackColor = Color.Green;
}
}
你可以根据你的条件判断哪行需要改变颜色,根据上边颜色设置下。
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string _value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "列的名字"));
if (_value == "0")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
{
if (Convert.ToInt32(e.Row.Cells[2].Text) == 0)
e.Row.BackColor = System.Drawing.Color.Red;
}
假如你上面的表结构绑定dataGridView后,是否合格是最后一列的话可以:
for(int i=0;i<dataGridView.Columns.Count;i++)
{
if(i!=2);//如果当前列不是【是否合格】这一列則不再进行下面的比较
continue;
for(int j=0;j<dataGridView.RowCount;j++)
{
if(dataGridView.Rows[j].Cells[i].FormatValue.ToString()=="0")
{
dataGridView.Rows[j].Cells[i].CellStyle.BackColor = Color.你想要的颜色;
}
}
}