Datagrid - Adding and updating records

DonnaF

Active member
Joined
Mar 20, 2003
Messages
30
I have a database for tasks. Based on the date selected, I retrieve all the related tasks for that date, and then bind the data to a datagrid. This works fine - the appropriate records are displayed for the selected date. However, Im very new at using datagrids, so Im not sure how to add new records, update records, or delete records on the database and reflect the changes on the datagrid. Can anyone help me with how to do this?

Heres how Im binding the data to the datagrid:

DataAdapter.SelectCommand.CommandText = _
"SELECT Task_Date, Task_Description FROM TasksTable2 " & _
"WHERE Task_Date = #" & newDate & "#"

DataSet1.Clear()
DataAdapter.Fill(DataSet1, "Tasks")
dgdTasks.SetDataBinding(DataSet1, "Tasks")



Thanks, Donna
 
Thanks everyone, for your responses. Ive now got the adding of new records to work, using the INSERT SQL Statement.

DataAdapter.InsertCommand.CommandText = _
"INSERT INTO TasksTable(Task_Date, Task_Description)" & _
"VALUES (" & SaveDate & "," & _
"" & txtDesc.Text & ")"
Try
DataAdapter.InsertCommand.ExecuteNonQuery()
Catch exceptionparameter As Exception
End Try


DataSet1.Clear()
DataAdapter.Fill(DataSet1, "Tasks")
dgdTasks.SetDataBinding(DataSet1, "Tasks")

However, Im not too clear on how to do an update, when a user clicks on an item in the datagrid, and changes it. Also, the key to the record is an autonumber field. How do I get to the current cell info that was changed, update the grid, and also update the record in the database? Any help would really be appreciated.

Thanks, Donna
 
If you have used the wizard to create the DataAdapter and Dataset, the insert update and delete commands have probably been written for you (expand your Region "Windows form designer generated code" to examine them). Once you have established the binding to the dataset, manipulation of the datagrid will automatically change the dataset. You need only call the update method of the dataadapter to update the source db. i.e. DataAdapter.update(DataSet1) You may also have to call a DataSet1.endedit prior to the update call.
If the insert, update and delete statements are not already written for you, you can use a commandbuilder object to create them (restricted to one table which must have a primary key) or you can write your own and associate them with the appropriate command object.

Jon
 
Back
Top