detecting changes in a datagridview

C

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?
 
J

Jack Jackson

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 To Market/DataGridView/DataGridView FAQ.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>
 
L

Linda Liu[MSFT]

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.DataGridViewEditingControlShowingEventArgs)
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

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.
 
C

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.
 
C

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.
 
C

Cor Ligthert[MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top