C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll show you how to use DataRow and DataColumn to update data in a DataTable. A DataTable is an in-memory representation of a table that can be used to perform various operations on data without interacting directly with the database.
Create a new C# Console Application project in Visual Studio and add the following namespaces:
using System; using System.Data;
First, let's create a DataTable with some sample data:
static DataTable CreateSampleDataTable() { DataTable table = new DataTable("Employees"); table.Columns.Add("EmployeeID", typeof(int)); table.Columns.Add("FirstName", typeof(string)); table.Columns.Add("LastName", typeof(string)); table.Rows.Add(1, "John", "Doe"); table.Rows.Add(2, "Jane", "Smith"); return table; }
Create a method called UpdateEmployeeName
that updates an employee's name in the DataTable:
static void UpdateEmployeeName(DataTable table, int employeeId, string newFirstName, string newLastName) { DataRow[] foundRows = table.Select($"EmployeeID = {employeeId}"); if (foundRows.Length > 0) { DataRow row = foundRows[0]; row["FirstName"] = newFirstName; row["LastName"] = newLastName; } }
Create a method called DisplayDataTable
to display the contents of the DataTable:
static void DisplayDataTable(DataTable table) { Console.WriteLine("EmployeeID\tFirstName\tLastName"); Console.WriteLine("---------------------------------------"); foreach (DataRow row in table.Rows) { Console.WriteLine($"{row["EmployeeID"]}\t\t{row["FirstName"]}\t\t{row["LastName"]}"); } }
In the Main
method, create a DataTable and update the data using the methods we've defined:
static void Main(string[] args) { DataTable employeeTable = CreateSampleDataTable(); Console.WriteLine("Before update:"); DisplayDataTable(employeeTable); UpdateEmployeeName(employeeTable, 1, "Johnathan", "Doe"); Console.WriteLine("\nAfter update:"); DisplayDataTable(employeeTable); Console.ReadLine(); }
C# Update DataTable with DataRow Example:
using System; using System.Data; class Program { static void Main() { // Create a DataTable DataTable dataTable = new DataTable("MyTable"); // Define columns DataColumn column1 = new DataColumn("ID", typeof(int)); DataColumn column2 = new DataColumn("Name", typeof(string)); // Add columns to DataTable dataTable.Columns.Add(column1); dataTable.Columns.Add(column2); // Create a DataRow DataRow newRow = dataTable.NewRow(); newRow["ID"] = 1; newRow["Name"] = "John Doe"; // Add the DataRow to DataTable dataTable.Rows.Add(newRow); // Modify the DataRow DataRow rowToUpdate = dataTable.Rows[0]; rowToUpdate["Name"] = "Updated Name"; // Display the DataTable foreach (DataRow row in dataTable.Rows) { Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}"); } } }
C# DataRow Set Field Value in DataTable:
DataRow
using the SetField
method.row.SetField("ColumnName", value);
Changing DataColumn Data Type in C# DataTable:
DataType
property of a DataColumn
to change its data type.column.DataType = typeof(newType);