views:

10

answers:

0

In my DataGridViewComboBoxColumn, I want to replace null value cells with DataGridViewTextBoxCell cells that basically say "this cell cannot be changed". The following code does this exactly the way I want:

foreach (DataGridViewRow row in dgv.Rows)
{
    if (row.Cells[9].Value == DBNull.Value)
    {
        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
        row.Cells[9] = cell;
        cell.Style.NullValue = "this cell is null";
        cell.ReadOnly = true;
    }
}

I notice that if the dgv is not visible when I run this code, the changes are applied but not retained. Changes are also lost if I sort a column. I can get around these issues by re-running the code, but I suspect there is a more elegant means of accomplishing what I am trying to accomplish. I'm fairly new to this, so please point me in the right direction!