3

The context here is I use a special kind of DataSource for DataGridView, that is an anonymous type which can be created as follow:

var data = new[] { new { A = "A", B = 1 }, new { A = "B", B = 2 }, new { A = "C", B = 3 }};

The problem is if I use the above data source for a DataGridView, I can't edit the content of the DataGridView starting by clicking on its cells.

myDataGridView.DataSource = data;

Could you please explain to me why and how to make it editable? Or simply we can't make it work that way?

Your help would be highly appreciated.

King King
  • 61,710
  • 16
  • 105
  • 130
  • I see that you are setting the DataSource but were are you actually `Binding` the DataSource... also can you show the code of where you are actually trying to edit the row.. also look at `myDataGridView.BeginEdit()` Method – MethodMan Apr 04 '13 at 18:11
  • No, I don't try editing its content programmatically, simply double-click on its cells (This behavior should allow user to edit the clicked cell). But as I said, it's impossible like as it's readable only (ReadOnly = true), but I'm sure the ReadOnly is false. Thanks – King King Apr 04 '13 at 18:16
  • `Nothing is Impossible` it would help if you would post the code that you have in the `DoubleClick` Event.. – MethodMan Apr 04 '13 at 18:17
  • You can only cast anonymous types as that of system.object, and those are read-only. – Derek Apr 04 '13 at 18:26
  • There is no code for DoubleClick event, why don't you try adding a DataGridView on a form, assign its DataSource with an anonymous type (as in my question) and run the project. That's all I do, no other code. If you mean I should add some code to DoubleClick event to work around this, I'll try, but if you try first and post your code here, I'm really appreciated for that. Thanks. – King King Apr 04 '13 at 18:27
  • @AseemGautman has answered this now – Derek Apr 04 '13 at 18:28

2 Answers2

6

Its because var data is anonymous type. And anonymous type properties are read-only.

Community
  • 1
  • 1
A G
  • 21,087
  • 11
  • 87
  • 112
1

I am not 100% sure about this but I think that you can't edit the grid because you are binding an anonymous type. Anonymous types cannot be changed.

Flavia
  • 563
  • 4
  • 9