0
votes

I am working with Win Forms and DataGridView control. My data gridview is not bound to a data source but columns and rows are added. On some condition I am making the grid view read only and trying to change the background color of each cell to gray. But the gray color appears and then goes back to white default.I am not able to understand why this is so.

Following are the events on the Data Grid View

  1. Cell End Edit
  2. EditingControlShowing

Code to make the grid readonly and set the background color

_dataGridView.ReadOnly=true;
foreach (DataGridViewRow row in _dataGridView.Rows)
        {
            row.DefaultCellStyle.BackColor = SystemColors.Control;

        }

Kindly suggest

1

1 Answers

1
votes

You can catch the RowPostPaint event of you Data Grid View.

In VB.NEt it looks like :

Public Sub repainWithGrayColor() Handles DataGridView1.RowPostPaint
If (condition) Then
   DataGridView1.Rows(e.RowIndex).Cells("ColumnName").Style.BackColor = Color.Gray
   DataGridView1.Rows(e.RowIndex).Cells("ColumnName").ReadOnly = True
 End If
End Sub

Hope it helped.