DataGridView / DataTable | rowState doesn't ch

2020-08-03 04:18发布

问题:

I'm having a problem with binding data to the DataGridView.

I have a class MyDataGridView that inherit from DataGridView. My data grid view data source is a BindingSource object that holds a DataTable as the data source.

Problem is, when I make changes via (my) DataGridView object - the rowState stays on 'unmodified' instead of modified; even though the values in the DataTable changed (that's what makes my update not work, since it consider the rowState as undefined). Notice that making changes directly to the DataTable works fine.

Thank you (:

回答1:

you must call the Validate method of the form that contains the grid. This method affect to grid as if you move the focus to another row in the grid.



回答2:

Ok, so after using google a little bit I found out that calling DataTable.Rows[index].EndEdit() solves the problem. I'm not sure if that's how I should do that but it solved the problem. (in order to call that method I use the DataGridView.CellEndEdit event).



回答3:

Can you show us the MyDataGrdView class code please?

and i think i already know the solution: you have to rebind the data source so the control knows when to update

call DataGridView.DataBind(); evrytime you have changed the data source. it's slightly possible you have to do this with your DataTable too.