By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

detecting changes in a datagridview

P: n/a
cj
As your probably aware the datagridview doesn't update it's source
datatable with changes to a cell until you move off that cell. Suppose
somone makes a change in a cell of my datagridview then clicks to go
elsewhere in the program--how can I be assured that change is
transmitted back to the source datatable?
Dec 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Wed, 05 Dec 2007 16:12:03 -0500, cj <cj@nospam.nospamwrote:
>As your probably aware the datagridview doesn't update it's source
datatable with changes to a cell until you move off that cell. Suppose
somone makes a change in a cell of my datagridview then clicks to go
elsewhere in the program--how can I be assured that change is
transmitted back to the source datatable?
One possibility is to force the changes to be made immediately,
although that won't work usefully if you have validation on the cell.

There is a lot of info about DataGridView here:
<http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc>

Section 3.2 describes the process for a checkbox column, and there is
a code snippet here
<http://207.46.236.188/MSDN/ShowPost.aspx?PostID=2105950&SiteID=1>

Googling for CurrentCellDirtyStateChanged has a number of hits, many
of which are not working at the moment but they may work later.

There is also some information here:
<http://www.msdner.com/dev-archive/106/2-7-1062147.shtm>
Dec 5 '07 #2

P: n/a
cj,

Call the EndEdit on the datasource.

Cor
As your probably aware the datagridview doesn't update it's source
datatable with changes to a cell until you move off that cell. Suppose
somone makes a change in a cell of my datagridview then clicks to go
elsewhere in the program--how can I be assured that change is
transmitted back to the source datatable?
Dec 6 '07 #3

P: n/a
Hi Chris,

When we select and type in the current cell in a DataGridView, the editing
control attached to the current cell is shown and the current cell is in
edit mode. If you click on another form in the application, only one event
is fired, i.e. the current editing control's LostFocus event.

So we could subscribe this event to get notified when the user clicks to go
elsewhere in the application so as to apply the changes in the current cell
back to the underlying data source.

The following is a sample to do this:

Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellValidated
If (Me.DataGridView1.EditingControl IsNot Nothing) Then
RemoveHandler DataGridView1.EditingControl.LostFocus, AddressOf
Control_LostFocus
End If
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
Handles DataGridView1.EditingControlShowing
AddHandler e.Control.LostFocus, AddressOf Control_LostFocus
End Sub

Sub Control_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
If (Me.DataGridView1.IsCurrentCellInEditMode) Then
RemoveHandler Me.DataGridView1.EditingControl.LostFocus,
AddressOf Control_LostFocus
Me.DataGridView1.EndEdit()
End If
End Sub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 6 '07 #4

P: n/a
cj
Thanks, but right now all that is information overload. Folks here are
aware you can build a UI in 10 minutes. What they don't understand is
what another user put so clearly on another forum "10 minutes to put a
UI on a database table, 3 hours messing around trying to get it to work
correctly." I don't have time to read all that. Thankfully my
tinkering and these questions is helping.

Jack Jackson wrote:
On Wed, 05 Dec 2007 16:12:03 -0500, cj <cj@nospam.nospamwrote:
>As your probably aware the datagridview doesn't update it's source
datatable with changes to a cell until you move off that cell. Suppose
somone makes a change in a cell of my datagridview then clicks to go
elsewhere in the program--how can I be assured that change is
transmitted back to the source datatable?

One possibility is to force the changes to be made immediately,
although that won't work usefully if you have validation on the cell.

There is a lot of info about DataGridView here:
<http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc>

Section 3.2 describes the process for a checkbox column, and there is
a code snippet here
<http://207.46.236.188/MSDN/ShowPost.aspx?PostID=2105950&SiteID=1>

Googling for CurrentCellDirtyStateChanged has a number of hits, many
of which are not working at the moment but they may work later.

There is also some information here:
<http://www.msdner.com/dev-archive/106/2-7-1062147.shtm>
Dec 6 '07 #5

P: n/a
cj
Yes, I find that does work but due to other situations in the code it
wasn't immediately apparent. I think with some rearranging it will do.

Cor Ligthert[MVP] wrote:
cj,

Call the EndEdit on the datasource.

Cor
>As your probably aware the datagridview doesn't update it's source
datatable with changes to a cell until you move off that cell.
Suppose somone makes a change in a cell of my datagridview then clicks
to go elsewhere in the program--how can I be assured that change is
transmitted back to the source datatable?
Dec 6 '07 #6

P: n/a
cj
Linda, sorry this pretty much goes over my head. Cor and some playing
around on my part have been able to make this a non-issue now. Thanks
anyway.

Linda Liu[MSFT] wrote:
Hi Chris,

When we select and type in the current cell in a DataGridView, the editing
control attached to the current cell is shown and the current cell is in
edit mode. If you click on another form in the application, only one event
is fired, i.e. the current editing control's LostFocus event.

So we could subscribe this event to get notified when the user clicks to go
elsewhere in the application so as to apply the changes in the current cell
back to the underlying data source.

The following is a sample to do this:

Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellValidated
If (Me.DataGridView1.EditingControl IsNot Nothing) Then
RemoveHandler DataGridView1.EditingControl.LostFocus, AddressOf
Control_LostFocus
End If
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
Handles DataGridView1.EditingControlShowing
AddHandler e.Control.LostFocus, AddressOf Control_LostFocus
End Sub

Sub Control_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
If (Me.DataGridView1.IsCurrentCellInEditMode) Then
RemoveHandler Me.DataGridView1.EditingControl.LostFocus,
AddressOf Control_LostFocus
Me.DataGridView1.EndEdit()
End If
End Sub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 6 '07 #7

P: n/a
Linda,

AFAIK does the push of a button not fire the lostfocus event. Armin can much
better tell this then me, however this has been forever with Microsoft
controls.

Cor

Dec 6 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.