C# – Disabling specific cell edit in DataGrid

cdatagridsilverlight

I need to know is it possible to disable a specific cell edit in a DataGrid, without disabling edit for the entire column in Silverlight 4. I can get the specific cell object as a FrameworkElement but it does not contain property IsReadOnly or IsEnabled.
You will probably ask: Why do I need that? Well my application requires disabling particular cells in row depending on the other cell content. Each row is being checked this way separately.
If you have an idea how I can achieve such a unusual behaviour please write 😉

Best Answer

If you have the row,column index of the cell/cells that you wish to have disabled:

int r = 2, c = 4;

Then you can listen to the events CellEnter and CellLeave and do the following:

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == r)
        {
            if (e.ColumnIndex == c)
            {
                dataGridView1.Columns[e.ColumnIndex].ReadOnly = true;
            }
        }
    }

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == r)
        {
            if (e.ColumnIndex == c)
            {
                dataGridView1.Columns[e.ColumnIndex].ReadOnly = false;
            }
        }
    }

You're still setting the entire column to Readonly, but since you are resetting it back after you leave the cell it has the effect of appearing to only work for the cell.

Related Topic