Before performing any operation on the DataTable you need to add the following namespace into your application.
Using System.Data
Introduction
DataTable is a Table which has rows and columns.
- using System;
- using System.Data;
- namespace OperationOnDataTable
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Initializes a new instance of the System.Data.DataTable class with the specified table name.
- DataTable tempTable = new DataTable();
- // Add columns in DataTable
- tempTable.Columns.Add("ID");
- tempTable.Columns.Add("NAME");
- // Add New Row in dataTable
- DataRow newrow = tempTable.NewRow();
- newrow[0] = 0;
- newrow[1] = "Avinash";
- tempTable.Rows.Add(newrow);
- // Add Another New Row in dataTable
- newrow = tempTable.NewRow();
- newrow[0] = 1;
- newrow[1] = "Kavita";
- tempTable.Rows.Add(newrow);
- //Now table is ready
- Console.WriteLine("Origenal table");
- for (int i = 0; i < tempTable.Rows.Count; i++)
- {
- Console.WriteLine(tempTable.Rows[i][0].ToString() + "--->" + tempTable.Rows[i][1].ToString());
- }
- // if you want edit data form TataTable
- Console.WriteLine("\nModified table");
- for (int i = 0; i < tempTable.Rows.Count; i++)
- {
- if (tempTable.Rows[i][1].ToString() == "Kavita")
- {
- tempTable.Rows[i][1] = "gare";
- }
- Console.WriteLine(tempTable.Rows[i][0].ToString() + "--->" + tempTable.Rows[i][1].ToString());
- }
- // if you want delete row form TataTable
- Console.WriteLine("\nAfter deleting row from table");
- for (int i = 0; i < tempTable.Rows.Count; i++)
- {
- if (tempTable.Rows[i][1].ToString() == "gare")
- {
- tempTable.Rows[i].Delete();
- }
- }
- for (int i = 0; i < tempTable.Rows.Count; i++)
- {
- Console.WriteLine(tempTable.Rows[i][0].ToString() + "--->" + tempTable.Rows[i][1].ToString());
- }
- Console.ReadLine();
- }
- }
- }
OUTPUT
Original table
0--->Avinash
1--->Kavita
Modified table
0--->Avinash
1--->gare
After deleting row from table
0--->Avinash

Amatya AgyeyPosted May 20, 2016, 1:15 AM
Nice 1.. Keep it up.. :)
Avinash AherPosted Sep 17, 2015, 2:19 PM
Thanks Steven
Steven TongPosted Sep 17, 2015, 10:58 AM
Thanks. It would be good to include code to add a row to the table.