我有这个代码附加在一个DataGridView的RowValidating事件
private void dgvSrc_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Rows[e.RowIndex].Cells["fullPath"].Value.ToString().Equals("none"))
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
else
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
}这是有效的,但不是自动的,您需要聚焦或首先单击,然后选择另一行,以查看其ForeColor是否更改为红色。我尝试使用不会自动格式化特定行的Update和Refresh。我该如何解决这个问题呢?
发布于 2013-06-11 14:42:32
使用DataGridView的CellFormatting事件
dgv.CellFormatting += dgv_CellFormatting像这样处理它
void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(dgv.Rows[e.RowIndex].Cells["fullPath"].Value.ToString().Equals("none"))
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
else
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
}发布于 2013-06-11 14:20:36
您订阅的事件不正确。我想,你需要这个:CellValueChanged
发布于 2013-06-12 09:06:18
您可以在您的dgv RowPrePaint事件中执行此操作。
Private void dgvSrc_RowPrePaint(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Rows[e.RowIndex].Cells["fullPath"].Value.ToString().Equals("none"))
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
else
dgv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
}https://stackoverflow.com/questions/17037452
复制相似问题