Datagridview单元格样式更新

Fra*_*sco 2 c# user-interface datagridview visual-studio-2010 winforms

我在 datagridview 上遇到了一个奇怪的问题。我需要更改所选单元格 (A) 的样式以响应另一个单元格 (B) = x 的值。(A) 是文本框,(B) 是组合框。我捕获了 CellEndEdit 事件,当用户更改 (B) 的值时,一切正常:(A) 的样式立即更改。

现在,当我尝试以编程方式更新 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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Gen*_*ent 5

据我所知,DataGridViews 的所有样式都需要在 DataGridView.CellFormatting 事件中发生。

此事件是您更改单元格默认样式/颜色的机会。