Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

By
Advertisement
In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.


Now before going to explain about how to do manipulations with datatable i will explain what datatable is - for the freshers who are not very familiar with it.

DataTable:

It represents one table of in-memory relational data, can be created and used independently, or can be used by other .NET Framework objects, most commonly as a member of a DataSet.

Creating DataTable & Adding Records:

Now i have created a new console application to explain the datatable manipulation. Datatable have different types of constructors. Based on our requirement we can use the specific constructor to create the datatable object. Below are the datatable constructors available.

DataTable() - This constructor is used to create new object of datatable with no arguments.
DataTable(String Tablename) - This constructor is used to create new object of the datatable with specified string as table name.
DataTable(String Tablename,String Namespace) - This constructor is used to create new object of the datatable with specified string as table name and specified namespace.

Now before creating instance of the datatable include the below namespaces if it is not already included.

using System;
using System.Data;

And now i have created the instance of the datatable with the table name as "Students" and inserted some sample records. And using the foreach loop i have just printed the dattable records.Below is the code.

// Initializes a new instance of the DataTable With Name
DataTable dTable = new DataTable("Students");

// Add columns with datatype in students datatable
dTable.Columns.Add("StudentId", typeof(int));
dTable.Columns.Add("StudentName", typeof(string));
dTable.Columns.Add("StudentClass", typeof(string));
dTable.Columns.Add("StudentSection", typeof(string));
dTable.Columns.Add("StudentRank", typeof(int));

// Add new Row to students datatable
DataRow newRow = dTable.NewRow();
newRow["StudentId"] = 1;
newRow["StudentName"] = "Kannadasan";
newRow["StudentClass"] = "10";
newRow["StudentSection"] = "A";
newRow["StudentRank"] = 1;
dTable.Rows.Add(newRow);

// Add new Row to students datatable
newRow = dTable.NewRow();
newRow["StudentId"] = 2;
newRow["StudentName"] = "Kaviyarasan";
newRow["StudentClass"] = "10";
newRow["StudentSection"] = "C";
newRow["StudentRank"] = 4;
dTable.Rows.Add(newRow);

// Add new Row to students datatable
newRow = dTable.NewRow();
newRow["StudentId"] = 3;
newRow["StudentName"] = "Xavier";
newRow["StudentClass"] = "10";
newRow["StudentSection"] = "D";
newRow["StudentRank"] = 3;
dTable.Rows.Add(newRow);

// Add new Row to students datatable
newRow = dTable.NewRow();
newRow["StudentId"] = 4;
newRow["StudentName"] = "Manju";
newRow["StudentClass"] = "10";
newRow["StudentSection"] = "B";
newRow["StudentRank"] = 2;
dTable.Rows.Add(newRow);

//Table Initial Data
Console.WriteLine("");
Console.WriteLine("Table Initial Data");
Console.WriteLine("=========================");
foreach (DataRow dRow in dTable.Rows)
{
    Console.WriteLine("Id : " + dRow["StudentId"].ToString() + ", Name : " + dRow["StudentName"].ToString() + ", Class : " + dRow["StudentClass"].ToString() + ", Section : " + dRow["StudentSection"].ToString() + ", Rank : " + dRow["StudentRank"].ToString());
}

Sorting Datatable Records:

We cann't sort the datatable directly since we don't have any predefined method to sort. So what we can do is create a new dataview from your datatable and apply the sorting you want to apply and then create a new datatable from the dataview using  DataView.ToTable() method.

Below is the code i have used to sort the datatable.

//Sorting Datatable
DataView dv = dTable.DefaultView;
dv.Sort = "StudentRank asc";
DataTable sortedTable = dv.ToTable();
Console.WriteLine("Sorted Data Table By Rank");
Console.WriteLine("=========================");
foreach (DataRow dRow in sortedTable.Rows)
{
    Console.WriteLine("Rank : " + dRow["StudentRank"].ToString()+", Id : " + dRow["StudentId"].ToString() + ", Name : " + dRow["StudentName"].ToString() + ", Class : " + dRow["StudentClass"].ToString() + ", Section : " + dRow["StudentSection"].ToString());
}

Updating & Deleting Datatable Records:

Below is the code i have used to update and delete the records in the datatable.

// Updating Student Record With Id 4
foreach (DataRow dRow in dTable.Rows)
{
    if (dRow["StudentId"].ToString() == "4")
    {
        dRow["StudentSection"] = "A";
        dRow["StudentName"] = "Manju Kannadasan";
        dRow["StudentRank"] = 1;

        Console.WriteLine("Modified Data");
        Console.WriteLine("=========================");
        Console.WriteLine("Id : " + dRow["StudentId"].ToString() + ", Name : " + dRow["StudentName"].ToString() + ", Class : " + dRow["StudentClass"].ToString() + ", Section : " + dRow["StudentSection"].ToString() + ", Rank : " + dRow["StudentRank"].ToString());
    }
}
Console.WriteLine("");

// Deleting Data With Id 2 & 3
for (int i = 0; i < dTable.Rows.Count; i++)
{
    if (dTable.Rows[i]["StudentId"].ToString() == "2" || dTable.Rows[i]["StudentId"].ToString() == "3")
    {
        dTable.Rows[i].Delete();
    }
}

Sample Output:

Now if you run the application you will get the below output.

Output of Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

Source Code:

Download the complete sourcecode in C#.Net below.
Sourcecode of Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net
You May Also Like.

If you like this article share it with your friends. Comment your suggestions on the comment section.

0 comments:

Post a Comment

Online Casino