Why can't I edit the values in my DataGridView, even though its not set to ReadOnly?
It turns out that if your DataGridView is bound to a ReadOnlyCollection, then even though you can programatically edit any item in the collection, the DataGridView will restrict you from changing the values. I'm not sure if this behavior is intentional, but its something to watch out for.
I installed VS 2013 just yesterday, latest build (update 5) and a bug still remains that causes the behavior you describe.
In short to work around the bug, first make sure the datagridview is set to be GUI-editable. This especially includes the tiny arrow in the form designer at the top right of the control. In the arrow drop down is an option "enable editing", ensure it's enabled. Now in the form designer edit the columns in some major manner (such as add or remove a column). That's it, when you run the program you should find the GUI editing is working now.
To reproduce this bug, within the form designer use the tiny arrow at the top right of the datagridview control to set "enable editing" to false. Now make a major change to the columns (such as add or remove a column). Compile and run the program. Now go back to the tiny arrow and re-enable "enable editing" checkbox. Again run the program. At this point the bug manifests itself, and you will find that the datagridview is not GUI-editable even though you've configured otherwise in the VS.
This is just an extended comment (hence wiki) in counter to the "the DataGridView will restrict you from changing some values (strings) but not other values (bools)" point; neither is editable; make it a List<T>
and both are editable...:
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
class Test
{
public string Foo { get; set; }
public bool Bar { get; set; }
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
var data = new ReadOnlyCollection<Test>(
new[] {
new Test {Foo="abc", Bar=true},
new Test {Foo="def", Bar=false},
new Test {Foo="ghi", Bar=true},
new Test {Foo="jkl", Bar=false},
});
Application.Run(
new Form {
Text = "ReadOnlyCollection test",
Controls = {
new DataGridView {
Dock = DockStyle.Fill,
DataSource = data,
ReadOnly = false
}
}
});
}
}