views:

51

answers:

1

Hi folks,

I'm trying to enabled editing for a number of columns in my DataGridView. Now, before anyone suggestions I should read the MSDN article: How to: Specify the Edit Mode for the Windows Forms DataGridView Control, I already have.

In summary (and I quote):

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

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

What I find interesting is the first line:-

The underlying data source supports editing.

Now, what I'm doing is the following, to bind the data to the 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;

Which I thought was the right way to do things..

What i was planing on doing was, when the cell is changed and the user then leaves the cell, that's where I was planning on grabbing the data that was just changed (figure this out manually) and then manually update the DB.

Is this the right way to do things?

+1  A: 

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

The members of an anonymous type are a sequence of read-only properties inferred from the anonymous object initializer used to create an instance of the type.

Instead, you might want to define a display class with editable properties for the values you want to display and create instances of that class.

adrift
Not sure .. considering that i was *projecting* the results into an anonymous object. (notice the line `select new { .. }` <-- has no class defined.
Pure.Krome
Ah, sorry, will edit my answer.
adrift
Perfect! That was the problem :) cheers!
Pure.Krome