Updating a Database through a Datagrid??

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi,

How do you update data that's been changed in a data grid column back to the
database? i am loading my datagrid as follows....

try {

BatchLstDs.Clear() ;

BatchLstConnection.ConnectionString = AppMain.CompanyConnectionPath ;

BatchLstConnection.Open() ;

//Establishing the database connection and loading the datagrid

SelectLstCommand.CommandText = "SELECT * FROM tblGLBatch ORDER BY " +
pOrderBy ;

SelectLstCommand.Connection = BatchLstConnection ;

BatchLstAdapter.SelectCommand = SelectLstCommand ;

BatchLstAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;

BatchLstAdapter.Fill(BatchLstDs, "tblGLBatch") ;

dgGLBatch.SetDataBinding(BatchLstDs, "tblGLBatch") ;

}

catch (Exception e) {

MessageBox.Show(e.Message, "PopulateDataGrid", MessageBoxButtons.OK,
MessageBoxIcon.Error) ;

}

finally {

BatchLstConnection.Close() ;

}


Regards

Darryn
 
Hi Darryn,

Set the DataAdapter's INSERT, UPDATE and DELETE commands, then simply call Update() on the DataAdapter and it will automatically insert, update and delete changes made to the dataset your datagrid is using.
 
I don't understand how i build an update or an insert command based on a
datagrid?? i can do it fine from textboxes can you lead me in the right
direction regrading how to do this? and how do i tell which to run the
update or the insert command because from my Datagrid you can add new and
edit existing records.. if a new record has been added and an old one
updated what do i do?


Morten Wennevik said:
Hi Darryn,

Set the DataAdapter's INSERT, UPDATE and DELETE commands, then simply call
Update() on the DataAdapter and it will automatically insert, update and
delete changes made to the dataset your datagrid is using.
 
No, you build an insert, update and delete command based on what you would to to a single line.

The dataadapter will call which one is necessary when you call the Update() method.

In the dataset rows will get marked deleted, new or changed as you edit the datagrid, and the dataadapter will use the insert command on all new rows, delete command on all deleted rows and all update commands on all updated rows.

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconupdatingdatabasewithdataadapterdataset.asp
 
Back
Top