Share via


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.

The following examples show how to locate and modify rows in both typed, and untyped datasets.

Editing a Record in a DataTable When You Do Not Know the Index of the Row That You Want To Edit

Typically, you do not know the index of the row you want to edit. Data tables in typed datasets are created with a FindBy method that uses the table's primary key to locate a row.

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

    In the following example, the CustomerID column is the primary key of the Customers table, so the generated FindBy method is FindByCustomerID. This example assumes that you have generated a typed dataset named NorthwindDataSet, and that you have an instance of this typed dataset named northwindDataSet1.

    Dim customersRow As NorthwindDataSet.CustomersRow
    customersRow = NorthwindDataSet1.Customers.FindByCustomerID("ALFKI")
    
    customersRow.CompanyName = "Updated Company Name"
    customersRow.City = "Seattle"
    
    NorthwindDataSet.CustomersRow customersRow = 
        northwindDataSet1.Customers.FindByCustomerID("ALFKI");
    
    customersRow.CompanyName = "Updated Company Name";
    customersRow.City = "Seattle";;
    

Typically, you do not know the index of the row you want to edit. Data tables in untyped datasets are created with a Select method that returns an array of DataRows.

To 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

    In the following example, the CustomerID column is the primary key of the Customers table, so calling the Select method and searching for the primary key will only result in finding one row. The return type is still an array of DataRows, so we access the (0) index, or first row, in the array. This example assumes that you have a dataset named dataSet1.

    Dim customerRow() As Data.DataRow
    customerRow = DataSet1.Tables("Customers").Select("CustomerID = 'ALFKI'")
    
    customerRow(0)("CompanyName") = "Updated Company Name"
    customerRow(0)("City") = "Seattle"
    
    DataRow[] customerRow = 
        dataSet1.Tables["Customers"].Select("CustomerID = 'ALFKI'");
    
    customerRow[0]["CompanyName"] = "Updated Company Name";
    customerRow[0]["City"] = "Seattle";
    

Editing a Record in a DataTable When You Know the Index of the Row That You Want To Edit

To update existing records using typed datasets (row index known)

  • Assign a value to the specific column within a DataRow object.

    Typed datasets utilize early binding, which exposes the table and column names as properties at design time. This results in code that is easier to both read and write.

    The following example shows how to update the data in the CompanyName and City columns of the fifth record in the Customers table in the dataset. This example assumes that you have generated a typed dataset named NorthwindDataSet, and that you have an instance of this typed dataset named northwindDataSet1.

    NorthwindDataSet1.Customers(4).CompanyName = "Updated Company Name"
    NorthwindDataSet1.Customers(4).City = "Seattle"
    
    northwindDataSet1.Customers[4].CompanyName = "Updated Company Name";
    northwindDataSet1.Customers[4].City = "Seattle";
    

To update existing records in untyped datasets (row index known)

  • Assign a value to a specific column within a DataRow object.

    The table and column names of untyped datasets are not available at design time and must be accessed through their respective indices.

    The following example shows how to update the data in the first two columns of the fifth record of the first table in dataSet1. This example assumes that the first table in dataSet1 corresponds to the Customers table of the Northwind database, and the first two columns of this table are the CompanyName and City columns.

    DataSet1.Tables(0).Rows(4).Item(0) = "Updated Company Name"
    DataSet1.Tables(0).Rows(4).Item(1) = "Seattle"
    
    dataSet1.Tables[0].Rows[4][0] = "Updated Company Name";
    dataSet1.Tables[0].Rows[4][1] = "Seattle";
    

    The following example shows how to update the same data as the preceding example, replacing the collection indices with table, and column names passed as strings. You still need to know the index of the row you want to edit.

    DataSet1.Tables("Customers").Rows(4).Item("CompanyName") = "Updated Company Name"
    DataSet1.Tables("Customers").Rows(4).Item("City") = "Seattle"
    
    dataSet1.Tables["Customers"].Rows[4]["CompanyName"] = "Updated Company Name";
    dataSet1.Tables["Customers"].Rows[4]["City"] = "Seattle";
    

See Also

Concepts

DataTable Edits

Adding Data to a DataTable

DataRow Deletion

Binding Windows Forms Controls to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

Other Resources

Connecting to Data in Visual Studio