2
votes

I'm developing a windows form application, in which I'm using a datagridview. It has some columns where ReadOnly is true and some where ReadOnly is false.

At a certain point, I have to prevent user to edit the datagridview, but still view all rows, so I set datagridview's ReadOnly property to true.

And when the datagridview's readonly state is set back to false, all columns' ReadOnly property is set to false as well.

I Can't disable the datagridview because in disabled state the user can't view all rows.

Thanks in advance.

3

3 Answers

1
votes

My original idea was to have you create a list of the ReadOnly columns for each DataGridView. Then have you reset those columns from that list, either in the ReadOnlyChanged event for each DataGridView (too much code copy), or send those lists as a second parameter to the common code method and reset them there. But then I thought, what if you only have access to the Common Code?

This led me to the following generic solution. The idea remains the same: each DataGridView will have a list of ReadOnly columns. But this list will be directly attached to the DataGridView via the Tag property instead of as code bloat in the dgv's (perhaps unreachable) parent class.

If your common code method looks something like this:

public void DoStuff(DataGridView dgv)
{
    if (dgv.ReadOnly)
    {
        dgv.ReadOnly = false;
        // Do related stuff.
    }
    else
    {
        dgv.ReadOnly = true;
        // Do other related stuff.
    }

    // Do common stuff.
}

Make these changes to Save / Load the ReadOnly columns:

public void DoStuff(DataGridView dgv)
{
    if (dgv.ReadOnly)
    {
        dgv.ReadOnly = false;

        // Load the saved ReadOnly columns.
        List<DataGridViewColumn> rocs = dgv.Tag as List<DataGridViewColumn>;

        if (rocs != null)
        {
            foreach (DataGridViewColumn roc in rocs)
            {
                roc.ReadOnly = true;
            }
        }

        // Do related stuff.
    }
    else
    {
        // Save the ReadOnly columns before the DGV ReadOnly changes.
        List<DataGridViewColumn> rocs = new List<DataGridViewColumn>();

        foreach (DataGridViewColumn col in dgv.Columns)
        {
            if (col.ReadOnly)
            {
                rocs.Add(col);
            }
        }

        dgv.Tag = rocs;
        dgv.ReadOnly = true;

        // Do other related stuff.
    }

    // Do common stuff.
}
0
votes

You can not revert the Column.ReadOnly state by just setting DataGridView.ReadOnly

You can save the ReadOnly columns into a List first and use a loop

foreach(var column in dgv.Columns.Cast<DataGridViewColumn>().Where(i => !ReadOnlyColumnList.Contains(i)))
{
    column.ReadOnly = false;
}
0
votes

I used the CellBeginEdit event, and check if the column is any of my read only columns, then cancel the event.

private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  //ReadOnly off/on enables this again - force non-editable
  if (e.ColumnIndex == 1 || e.ColumnIndex == 5) e.Cancel = true;
}