2
votes

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;
            }
        }
    }
2
can you show the disableStyle and enableStyle codes - Sandy
this.enableStyle = new DataGridViewCellStyle() { ForeColor = Color.Black, SelectionBackColor = Color.LightSteelBlue }; this.disableStyle = new DataGridViewCellStyle() { ForeColor = Color.White, SelectionBackColor = Color.White }; - Francesco

2 Answers

4
votes

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.

0
votes

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