我正面临着datagridview的一个奇怪问题。 我需要改变所选单元格(A)的样式以响应另一个单元格(B)= x的值。 (A)是一个文本框,而(B)是一个组合框。 我发现事件CellEndEdit,当用户更改(B)的值时,一切正常:(A)的样式立即改变。Datagridview单元格样式更新
现在,当我尝试以编程方式更新datagridview时,这不起作用。 奇怪的是,两种方法共享同一个方法UpdateTimeChannelCell。 如果我以编程方式调用此方法,则datagridview不会更新其单元格的样式。我试图通过更新,令人耳目一新,在DataGridView没有运气
private void UpdateTimeChannelCell(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 || e.ColumnIndex == 3 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 9 || e.ColumnIndex == 11 || e.ColumnIndex == 13)
{
if ((int)this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == 0)
{
this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = new Time();
this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Style = disableStyle;
}
else
{
this.dataGridView_TidKanaler.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Style = enableStyle;
}
}
}
可以显示disableStyle和enableStyle代码 – Sandy
this.enableStyle = new DataGridViewCellStyle(){ForeColor = Color.Black,SelectionBackColor = Color.LightSteelBlue}; this.disableStyle = new DataGridViewCellStyle(){ForeColor = Color.White,SelectionBackColor = Color.White}; – Francesco