更改 DataGridView 的行背景颜色
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 row's background color is not changing
m.lansers回答正确。你有没有把他用的方法绑定到DataGridViews的CellFormatting事件上?如:
dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(grid1_CellFormatting);
此外,另一种较短的代码可以使用:
e.CellStyle.BackColor = Color.Blue;
我搜索了很多,找到了一些在数据网格视图中更改行背景颜色的方法,但根本不起作用,你能帮我找出我的错误吗?
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 row's background color is not changing
m.lansers回答正确。你有没有把他用的方法绑定到DataGridViews的CellFormatting事件上?如:
dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(grid1_CellFormatting);
此外,另一种较短的代码可以使用:
e.CellStyle.BackColor = Color.Blue;