繁体   English   中英

Datagridview单元格样式更新

[英]Datagridview cell style update

我在使用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;
            }
        }
    }

据我所知,DataGridViews的所有样式都需要在DataGridView.CellFormatting事件内进行。

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

我不得不强制刷新网格以更新样式。 例如。 this.dataGridView_TidKanaler.Refresh()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM