Rémi
Rémi

Reputation: 3967

DataGridView Readonly "bug"

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?

Upvotes: 3

Views: 5639

Answers (3)

to StackOverflow
to StackOverflow

Reputation: 124794

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.

Upvotes: 4

Weston Odom
Weston Odom

Reputation: 311

If you fill the DataGridView manually with code rather than binding it to a DataSource then you can simply set a row's readonly property to true when you add it.

If the above won't work then I don't understand what your code actually does, and like @Dilshod said it would be convenient if you posted it, or at least linked to a gist of it (http://www.gist.github.com).

Upvotes: 0

Dilshod
Dilshod

Reputation: 3331

if the class is implemented by yourself then you can set your DataGrid1.ReadOnly = true and make the properties ReadOnly which needs to be ReadOnly.

like this:

string _myProperty;
public string MyProperty
{
   get{return _myProperty;}
}

Upvotes: 0

Related Questions