Francesco
Francesco

Reputation: 964

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

Upvotes: 2

Views: 3632

Answers (2)

Sanvir Manilal
Sanvir Manilal

Reputation: 937

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

Upvotes: 0

Gent
Gent

Reputation: 2685

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.

Upvotes: 5

Related Questions