我有数据网格视图有2列,即文件名和状态 .DataGridview有一个像这样的数据源设置
var bs = new BindingSourceAsync();
bs.DataSource = data;
dataGridView4.DataSource = bs;
使用async-await方法更新状态列的值。当状态值为"无效"我需要将相应的行颜色更改为红色和绿色,如果它的"有效"。
为此我尝试挂钩到DataGridView的CellValueChanged事件
dataGridView4.CellValueChanged += DataGridView4_CellValueChanged;
但事件永远不会被解雇。我怎么能解决这个问题。请指教。
答案 0 :(得分:1)
是的,在Gridview的RowDataBound中
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if it is the DataRow not the header row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Cell[1] is the cell contain the value for the condition
if (Convert.ToInt16(e.Row.Cells[1].Text) < 10)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
}
else if (Convert.ToInt16(e.Row.Cells[1].Text) < 30)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Blue;
e.Row.Cells[1].BackColor = System.Drawing.Color.Blue;
}
}
}