Alex
Alex

Reputation: 4948

How to Update DataTable

I have a main DataSet which contains several DataTables. These different DataTables are bound to their DataGridView's DataSource respectively.

My problem is whenever I modify something in the display area such as the description textbox below and then click on save....

Description <<< To >>> Modified Description

The DataGridView doesn't have the replicated changes, as seen here: DataGridView Results

I need a way to update the DataTable ... My save button successfully saves the information. I am quite new to DataSets and DataTables so this is my first time trying to update a DataTable. I doubt I need to reload the information in the DataTable, there must be something more efficient?

Upvotes: 3

Views: 30737

Answers (1)

Alex
Alex

Reputation: 4948

Updating a DataTable With Unknown Indexes

For more information: How to: Edit Rows in a DataTable

In order to edit an existing row in a DataTable, you need to locate the DataRow you want to edit, and then assign the updated values to the desired columns.


Update existing records in typed datasets (Row index not known)

Assign a specific DataRow to a variable using the generated FindBy method, and then use that variable to access the columns you want to edit and assign new values to them.

Dim Description As String = "Hello World Modified"

'Update DataTable
Dim Row As DataSet1.DataTableRow
Row = DataSet1.DataTableRow.FindByPrimaryKey(PK)
Row.Description = Description

Update existing records in untyped datasets (Row index not known)

Use the Select method of the DataTable to locate a specific row and assign new values to the desired columns

Dim Description As String = "Hello World Modified"

'Update DataTable
Dim Row() As Data.DataRow
Row = DataSet1.Tables("Table1").Select("PrimaryKey = '10'")
Row(0)("Description") = Description

Once this is done, I don't need to refresh anything else - my DataGridView has the latest information.

Upvotes: 7

Related Questions