Export (0) Print
Expand All

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.

    NorthwindDataSet.CustomersRow customersRow = 
        northwindDataSet1.Customers.FindByCustomerID("ALFKI");
    
    customersRow.CompanyName = "Updated Company Name";
    customersRow.City = "Seattle";;
    
    
    NorthwindDataSet.CustomersRow customersRow = 
        northwindDataSet1.get_Customers().FindByCustomerID("ALFKI");
    
    customersRow.set_CompanyName("Updated Company Name");
    customersRow.set_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.

    DataRow[] customerRow = 
        dataSet1.Tables["Customers"].Select("CustomerID = 'ALFKI'");
    
    customerRow[0]["CompanyName"] = "Updated Company Name";
    customerRow[0]["City"] = "Seattle";
    
    
    DataRow[] customerRow = 
        dataSet1.get_Tables().get_Item("Customers").Select("CustomerID = 'ALFKI'");
    
    customerRow[0].set_Item("CompanyName", "Updated Company Name");
    customerRow[0].set_Item("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:

    northwindDataSet1.Customers[4].CompanyName = "Updated Company Name";
    northwindDataSet1.Customers[4].City = "Seattle";
    
    
    northwindDataSet1.get_Customers().get_Item(4).set_CompanyName("Updated Company Name");
    northwindDataSet1.get_Customers().get_Item(4).set_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, accessing the data values with the tables, rows, and items (columns) collection indices:

    dataSet1.Tables[0].Rows[4].ItemArray[0] = "Updated Company Name";
    dataSet1.Tables[0].Rows[4].ItemArray[1] = "Seattle";
    
    
    dataSet1.get_Tables().get_Item(0).get_Rows().get_Item(4).
        get_ItemArray().set_Item(0, "Updated Company Name");
    
    dataSet1.get_Tables().get_Item(0).get_Rows().get_Item(4).
        get_ItemArray().set_Item(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]["CompanyName"] = "Updated Company Name";
    dataSet1.Tables["Customers"].Rows[4]["City"] = "Seattle";
    
    
    dataSet1.get_Tables().get_Item("Customers").get_Rows().get_Item(4).
        set_Item("CompanyName", "Updated Company Name");
    
    dataSet1.get_Tables().get_Item("Customers").get_Rows().get_Item(4).
        set_Item("City", "Seattle");
    
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft