Unable to edit any columns in my C # DataGridView - does anyone know why?

I am trying to enable editing for multiple columns in my DataGridView. Now, before any suggestions, I should read the MSDN article : How to: Set the editing mode for the Windows Forms DataGridView control , I already have it.

In conclusion (and quote):

  • The main data source supports editing.
  • The DataGridView control is enabled.
  • The value of the EditMode property is not EditProgrammatically.
  • The ReadOnly properties of the cell, row, column, and control are set to false.

All this is simple and common sense. I confirmed that Enabled = true . I confirmed that EditMode is EditOnKeystrokeOrF2 I confirmed that all columns (except one) are ReadOnly = false .

What interests me is the first line: -

The main data source supports editing.

Now, I do the following: bind data to DGV: -

 // Grab all the Foos. var foos = (from x in MyRepository.Find() select new { x.Foo1, x.Foo2, ... x.FooN }).ToList(); // Now lets bind this result to the GridView. dataGridView2.DataSource = foos; 

I thought this was the right way to do something.

What I planned to do was when the cell was changed, and the user left the cell where I planned to capture the data that had just been changed (this can be seen manually), and then manually update the database.

Is this right to do?

+4
source share
2 answers

In this case, the main data source does not support editing, since the properties of anonymous types are read only. In the C # language specification:

Elements of an anonymous type are a series of read-only properties derived from the initializer of an anonymous object used to instantiate this type.

Instead, you can define a display class with editable properties for the values ​​you want to display, and instantiate that class.

+3
source

I had this problem with unrelated DGV. I set it by setting the ReadOnly property for each ROW. Both the DGV and the columns were not ReadOnly , but the rows were somehow set by R / O, possibly when I installed them using the collection.

So:

 foreach (var r in Lst) { ... dgv.Rows[dgv.Rows.Count - 1].ReadOnly = false; } 
+2
source

All Articles