I have a datagridview to display some data. Some rows between data are separator rows so those are readonly. In some cases the whole datagridview might be readonly. But when I switch it back to readonly = false, all the rows are editable. Is it possible with out having to set again the readonly property of each row manually that my row came back as they were before?
As far as I can see using Reflector, setting DataGridView.ReadOnly to true will also set ReadOnly to false for all rows and columns in the grid - presumably it is assumed that you'll never subsequently want to set DataGridView.ReadOnly to false again.
So the only way I can see for you to get round this, is to "remember" which rows should be ReadOnly, for example by setting a suitable value in DataGridViewRow.Tag, then using this to restore the ReadOnly state manually.
For example, if you've set the DataGridViewRow.Tag property to true for readonly rows, you could handle the DataGridView.ReadOnlyChanged event with a handler that looks something like the following untested code:
void DataGridView_ReadOnlyChanged(object sender, EventArgs e)
{
DataGridView dataGridView = (DataGridView) sender;
if (!dataGridView.ReadOnly)
{
// DataGridView.ReadOnly has just been set to false, so we need to
// restore each row's readonly state.
foreach(DataGridViewRow row in dataGridView.Rows)
{
if (row.Tag != null && ((bool)row.Tag))
{
row.ReadOnly = true;
}
}
}
}
However it seems clear that the DataGridView isn't designed to allow its ReadOnly property to be toggled in that way. Maybe you could design your application so that you don't ever need to set DataGridView.ReadOnly to true?
For example, if you want to prevent a user from editing by double-clicking on a cell, you could set DataGridView.EditMode to DataGridViewEditMode.EditProgramatically instead of setting DataGridView.ReadOnly to true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With