views:

289

answers:

1

I am changing DataGridView cell values programmatically, but values are not pushed to bound datasource. They are pushed only for cells belonging to selected row. How can I ask the DataGridView to push rows values into datasource?

EDIT:

This code seems to do the trick, but may be some better solution?

grid.CurrentCell = cell;
cell.Value = "some value";
grid.EndEdit(0);
A: 

You shouldn't update the DataGridView cells, instead you should directly update the data source. If it implements INotifyPropertyChanged, the DataGridView will reflect the changes automatically

Thomas Levesque
Hm... Why i SHOULD NOT do this? I would like to paste some values into DataGridView and want them to be propagated into datasource.
alex2k8
Why ? Many reasons... For instance, doing that introduces coupling between UI and business concerns. If someday you want to port your app to WPF, ASP.NET or anything else, it will be much more work if the business-related code is mixed with UI-related code. Also, the DataGridView is designed to be modified by the user, not by code (hence your problems). And anyway it is usually simpler to modify the data source than the UI itself...
Thomas Levesque
Looks like you assume a bit different scenarios than I. Consider user wants to do a simple copy/paste for some rows. DataGridView has no built in support for this, so I have a code todo this. I get data from Clipboard and push into grid. I agree, there are two options: put data into datasource or put into the View. I don't want to care about parsing string values and pushing them into correct fields of data objects manually. So no need to update codee if columns order change. Seems in my case is simpler to put data into cells. So thanks for you ideas, but I guess not what I am looking for.
alex2k8