Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid and if changed data

Status
Not open for further replies.

Coppermill

Programmer
Oct 5, 2004
21
GB
Simple question how do you know if a DataGrid has data which has changed.

I need to do this if a use hits close without saving the data first.
 
My feeling is taht you would look at the underlying datasource if it is a datatable or dataset and that this should easily give you all the informatioin you are looking for..

The datatable has a property/filter that lets you check for modified rows. (inserts/updates/deletes)

If you arn't using a datatable/dataset you might want to save modifications as soon as the users finishes an edit.

HTH


Rob
 
No the user has the ability to cancel everything he was doing, and therefore has a save button too.
 
Create a new project with a form (form1) and a datagrid (dgContacts). Paste the following (remembering to alter the sql and connection strings).

Any data changed when the form is closed will be detected and the command builder is used to save the changes. This only works on DataGrids based on single table not views mind. It may need some tweaking as I haven't tested this in a while but it should give you a good start!

Cheers
Andrew

________________

Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

Private Const SELECT_STRING As String = "insert SQL select statement here"
Private Const CONNECT_STRING As String = "insert connection string here"
Private m_DataSet As DataSet

' Load the data.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim data_adapter As SqlDataAdapter
data_adapter = New SqlDataAdapter(SELECT_STRING, CONNECT_STRING)

data_adapter.TableMappings.Add("Table", "Contacts")
m_DataSet = New DataSet()
data_adapter.Fill(m_DataSet)

dgContacts.SetDataBinding(m_DataSet, "Contacts")
dgContacts.DataSource = m_DataSet.Tables("Contacts")
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If m_DataSet.HasChanges() Then
Dim data_adapter As SqlDataAdapter
Dim command_builder As SqlCommandBuilder

data_adapter = New SqlDataAdapter(SELECT_STRING, CONNECT_STRING)

data_adapter.TableMappings.Add("Table", "Contacts")
command_builder = New SqlCommandBuilder(data_adapter)

data_adapter.Update(m_DataSet)
End If
End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top