ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,jQuery Plugins,jQuery UI,SSRS,XML,HTML,jQuery demos,code snippet examples.

Breaking News

  1. Home
  2. C #
  3. C# DataTable

C# DataTable

DataTable is like a container that is used to store some things. It is a collection of rows and columns of data.This article explains how to use DataTable in C#? Summary of the article:
  • What is DataTable?
  • How to Add Data in a DataTable?
  • How to Select Data From DataTable in C#?
  • How to Convert DataTable to XML in C#?
  • How to Convert XML to DataTable in C#?
  • How to Convert List to DataTable in C#?
What is DataTable?
In our application we need to store and retrieve data from the database. By using DataTable we can store data from any source. It is a powerful technology or idea to store data in memory. We can perform CRUD operations in DataTable. We can also use the data of DataTable in our data source for our control like GridView Or DataGridView. DataTable provides four columns of type int, string and DateTime.
How to Add Data in a DataTable?
C# code to save data in a DataTable is given bellow:
DataTable oDataTable = new DataTable();
oDataTable.Columns.Add("StudentID",typeof(int));
oDataTable.Columns.Add("StudentName", typeof(string));
oDataTable.Columns.Add("Address", typeof(string));
oDataTable.Columns.Add("Phone", typeof(string));

oDataTable.Rows.Add(1,"John Smith","London","44669922");
oDataTable.Rows.Add(2, "Kamal Hyder", "Khulna", "88669977");
oDataTable.Rows.Add(3, "Pinal Roy", "Mumbai", "96885522");
How to Select Data From DataTable in C#?
DataTable has a Select method as like SQL Select operation to select specific rows based on condition.Consider the following example. Here at first we will add some data in a DataTable. Then we will select a row whose address is Khulna.
DataTable oDataTable = new DataTable();
oDataTable.Columns.Add("StudentID",typeof(int));
oDataTable.Columns.Add("StudentName", typeof(string));
oDataTable.Columns.Add("Address", typeof(string));
oDataTable.Columns.Add("Phone", typeof(string));

oDataTable.Rows.Add(1,"John Smith","London","44669922");
oDataTable.Rows.Add(2, "Kamal Hyder", "Khulna", "88669977");
oDataTable.Rows.Add(3, "Pinal Roy", "Mumbai", "96885522");

string StudentID;
string StudentName;
string Address;
string Phone;

DataRow[] oDataRow = oDataTable.Select("Address='Khulna'");
foreach (DataRow dr in oDataRow)
{
    StudentID   = dr[0].ToString();
    StudentName = dr[1].ToString();
    Address     = dr[2].ToString();
    Phone       = dr[3].ToString();
}
How to Convert DataTable to XML in C#?
We can convert a DataTable into a XML file. For this at first need to add some data in our DataTable. Then following C# code will convert our DataTable into XML documents.
DataTable oDataTable = new DataTable();
oDataTable.Columns.Add("StudentID",typeof(int));
oDataTable.Columns.Add("StudentName", typeof(string));
oDataTable.Columns.Add("Address", typeof(string));
oDataTable.Columns.Add("Phone", typeof(string));

oDataTable.Rows.Add(1,"John Smith","London","44669922");
oDataTable.Rows.Add(2, "Kamal Hyder", "Khulna", "88669977");
oDataTable.Rows.Add(3, "Pinal Roy", "Mumbai", "96885522");

oDataTable.TableName = "MyTable";
oDataTable.WriteXml(Server.MapPath("Test.xml"));
How to Convert XML to DataTable in C#?
We can convert a XML file into DataTable. And we can use XML file as our data source for control like GridView Or DataGridView. The following C# code will help us. Here “Test.xml” is a XML file.
oDataTable.ReadXml(Server.MapPath("Test.xml"));
GridView1.DataSource = oDataTable;
GridView1.DataBind();
How to Convert List to DataTable in C#?
We can convert a list into a DataTable. We can also use this DataTable as our data source. The following C# code will help use. Here “ConvertListToDataTable” is a method used to convert a list into a DataTable.
// Example of a list
List<string[]> oList = new List<string[]>();
oList.Add(new string[] { "Data 1", "Data 2", "Data 3" });
oList.Add(new string[] { "Data 1", "Data 2", "Data 3" });
oList.Add(new string[] { "Data 1", "Data 2", "Data 3" });

// Convert to DataTable
DataTable oDataTable = ConvertListToDataTable(oList);
GridView1.DataSource = oDataTable;
GridView1.DataBind();

static DataTable ConvertListToDataTable(List<string[]> oList)
{
    // New table
    DataTable oDataTable = new DataTable();

    // Get max columns
    int columns = 0;
    foreach (var array in oList)
    {
        if (array.Length > columns)
        {
            columns = array.Length;
        }
    }

    // Add columns
    for (int i = 0; i < columns; i++)
    {
        oDataTable.Columns.Add();
    }

    // Add rows
    foreach (var array in oList)
    {
        oDataTable.Rows.Add(array);
    }

    return oDataTable;
}
That’s all about C# DataTable

No comments