简体   繁体   中英

Datagridview cell style update

I'm facing a strange problem with a datagridview. I need to change the style of a selected cell (A) in response to the value of another cell (B) = x. (A) is a textbox while (B) is a combobox. I catch the event CellEndEdit and everything works fine when the user changes the value of (B): the style of (A) changes immediately.

Now, when I try to update the datagridview progammatically, this does not work. The strange thing is that both ways share the same method, UpdateTimeChannelCell. If I call this method programmatically, the datagridview does not update the style of its cells. I tried by updating, refreshing, invalidating the datagridview with no luck

        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;
            }
        }
    }

As far as I know all Styling for DataGridViews needs to occur within the DataGridView.CellFormatting event.

This event is your opportunity to change the default style/coloring of cells.

I had to force refresh my grid to get the styles to update. eg. this.dataGridView_TidKanaler.Refresh()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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