设置datagridview 单个单元格的背景色

方法一:

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex]; 
try
{
if (dgr.Cells["onduty1"].Value.ToString() == "" || dgr.Cells["onduty1"].Value.ToString() == null)
{
dgr.Cells["onduty1"].Style.BackColor = Color.Red;
}
 
}
catch (Exception ex)
{

 MessageBox.Show(ex.Message); 
}
}

方法二:

dataGridView1.Rows[index].Cells["测试结果"].Style.BackColor = Color.White;

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex]; 
try
{
if (dgr.Cells["onduty1"].Value.ToString() == "" || dgr.Cells["onduty1"].Value.ToString() == null)
{
dgr.Cells["onduty1"].Style.BackColor = Color.Red;
}
 
}
catch (Exception ex)
{

 MessageBox.Show(ex.Message); 
}
}

方法二:

dataGridView1.Rows[index].Cells["测试结果"].Style.BackColor = Color.White;

猜你喜欢

转载自www.cnblogs.com/wfy680/p/12044027.html
今日推荐