How can I force a commit on the current row from within a virtual unbound DataGridView's edit control event handler?
Using the walkthrough from [[http://msdn.microsoft.com/de-de/library/15a31akc.aspx]], I implemented a DatagridView
in virtual mode, totally unbound, in VB 10 Express (we are talking WinForms).
I understand that CommitEdit (DataGridViewDataErrorContexts.Commit)
commits the current cell's contents to the data cache.
The example uses row-level commit, i.e. it pushes the values into a row buffer until the current row is left. Only then the row is added to (or updated in) the data store. That's exactly what I do, too.
Question -- how can I commit the current row programmatically without leaving it? (I want to commit the whole row, not just the current cell.)
If the user leaves the row, there is a RowValidating
, RowValidated
, RowEnter
event sequence where the datastore update is taken care of. So one idea (currently my only one) was to simply re-set CurrentCell
to Nothing
, for example.
But I cannot just set the DataGridView
's CurrentCell
property to Nothing
(or any cell outside of the current row) since I want to commit from within an event handler of a custom edit control created by a custom column, and I see an System.InvalidOperationException
exception when setting CurrentCell
there.
In fact, it is a tiny button right to a combo box, i.e. I create a DataGridViewComboBoxCell
descendant which is a DataGridViewComboBoxColumn
descendant's edit control. My special ComboBox cell adds a tiny button which should commit the current row. Unfortunately, that button's _Click
event handler cannot change CurrentCell
since DetachEditingControl
throws the exception then.
Obviously, I must delay the CurrentCell
re-assignment until the whole _Click
event has been processed. Or what? How would I do that -- register a custom Windows message, SendMessage
that one to me, and override the windows procedure of the form so it re-sets CurrentCell
when this message is received? (Hello?)
There should be a rather easy way to let DataGridView
commit all changes by firing the events that I handle to perform the datastore update, no?
I hope it is somewhat clear what I am talking about. I do not have a comp开发者_运维知识库act code example yet :(
Here is a reliable way to commit the current row:
' Optional: mark the current cell as dirty so the current row definetely will be
' commited:
MyDataGridView.NotifyCurrentCellDirty(True)
' Aktuelle Row commiten. Einen besseren Weg als "Keine Zelle selektieren, dann Originalzelle wieder selektieren" kenne ich nicht:
Dim OrigCellAddress As Point = New Point(MyDataGridView.CurrentCellAddress.X, MyDataGridView.CurrentCellAddress.Y)
MyDataGridView.CurrentCell = Nothing
MyDataGridView.CurrentCell = MyDataGridView.Rows(OrigCellAddress.Y).Cells(OrigCellAddress.X)
The problem with the exception was a BeginEdit/EndEdit sequence i perform in my RowEnter event handler :( which resulted in an indirect recursive call of non-reentrant stuff.
精彩评论