[英]Changing the row background color of DataGridView
我搜索了很多,并找到了一些方法来改变数据网格视图中的行背景颜色,但它根本不起作用,你能帮我找到我的错误吗?
dataGridViewResult.DataSource = morgan.GetResult().Tables[5];
dataGridViewResult.Rows[0].Cells[1].Style.ForeColor = System.Drawing.Color.Beige;
dataGridViewResult.Rows[0].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[7].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[40].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[11].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[19].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[28].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[35].DefaultCellStyle.ForeColor = Color.Blue;
dataGridViewResult.Rows[35].DefaultCellStyle.ForeColor = Color.White;
此代码可能有效:
private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
// check the cell value under your specific column and then you can toggle your colors
row.DefaultCellStyle.BackColor = Color.Green;
}
更多信息: DataGridView行的背景颜色没有变化
m.lansers答案是对的。 您是否将他使用的方法绑定到DataGridViews CellFormatting事件? 如:
dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(grid1_CellFormatting);
另外,另一个较短的代码就是使用:
e.CellStyle.BackColor = Color.Blue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.