Modify or Edit Rows in a DataTable C# VB.Net

In DataTable you can Modify or Edit any row’s column data by specifying either row & column index number or column name. To edit an existing row in a DataTable, you need to locate or identify the DataTable record you want to edit, and then assign the updated values to the desired columns. After changes made to data in a DataTable, you can accept the changes using the AcceptChanges method of the DataRow, DataTable, or DataSet.

To Do the Example First Create a DataTable:
C# Code:

            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));
            oTable.Columns.Add(new DataColumn("Published", typeof(System.DateTime)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now);
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now);
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now);

Note: Don’t forget to add “using System.Data;” Namespace;

VB.Net Code:

            Dim oTable As DataTable = New DataTable("Article")

            'Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int64")))
            oTable.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
            oTable.Columns.Add(New DataColumn("Published", System.Type.GetType("System.DateTime")))

            'Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now)
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now)
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now)

Note: Don’t forget to add “Imports System.Data” Namespace;

Edit DataRow Using Row & Column Index Number:
C# Code:

            // Assign the value
            oTable.Rows[2][1] = "Modify Master Detail Data";
            oTable.AcceptChanges();

VB.Net Code:

            'Assign the value
            oTable.Rows(2)(1) = "Modify Master Detail Data"
            oTable.AcceptChanges()

Edit DataRow Using Row Index & Column Name:
C# Code:

            // Assign the value
            oTable.Rows[2]["Title"] = "Modify Master Detail Data";
            oTable.AcceptChanges();

VB.Net Code:

            'Assign the value
            oTable.Rows(2)("Title") = "Modify Master Detail Data"
            oTable.AcceptChanges()

If Row index not known:
Use Select method to find a Row from a DataTable. Select Method return an array of DataRows. So we can access the (0) index, or first row, from the array if we search through Primary Key.
C# Code:

            DataRow[] oRow = oTable.Select("ID = 1003");
            oRow[0]["Title"] = "Modify Master Detail Data";
            oRow[0].AcceptChanges();

VB.Net Code:

            Dim oRow() As DataRow = oTable.Select("ID = 1003")
            oRow(0)("Title") = "Modify Master Detail Data"
            oRow(0).AcceptChanges()

Note: To know more about Select Method. Click Here.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*