c sharp

What is DataTable C#

In C#, a DataTable is a fundamental object used to represent tabular data. It is a set of rows and columns that can be used to organize and work with structured data. In this article, we will discuss the syntax of a DataTable in C# and how to use it with an example.

What is DataTable in C#

As mentioned above, DataTable is a fundamental object that is a collection of rows and columns used to store and manipulate data in a structured format. A DataTable can be used to represent a variety of data structures, such as a database table, a spreadsheet, or a CSV file. It allows developers to create and manage complex data structures with ease.

DataTable class provides methods for adding and deleting rows and columns, sorting and filtering data, and performing other operations on the data stored in the table. The following is the basic syntax for creating a DataTable in C#:

DataTable dt = new DataTable();

 
This statement creates a new instance of the DataTable class and assigns it to the variable dt. After creating the DataTable object, it’s time to declare number of columns and number of rows to the list.

Adding Columns in DataTable

We can add columns to a DataTable using the Columns property, the following code lines add three columns to the DataTable:

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));

 
Here, we first specify the name of the column, followed by the data type of the column.

Adding Rows in DataTable

We can add rows to a DataTable using the Rows property, the following code adds two rows to the DataTable:

dt.Rows.Add(1, "Sam", 25);
dt.Rows.Add(2, "Walker", 30);

 
Here, the values for each column in the row are specified.

Using DataTable in C#

Here is an example code of DataTable that displays a list of people and their ages:

using System;
using System.Data;

class Program
{
    static void Main()
    {
       
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));

     
        dt.Rows.Add("Sam", 25);
        dt.Rows.Add("Walker", 30);

       
        foreach(DataRow row in dt.Rows)
        {
            Console.WriteLine(row["Name"] + " is " + row["Age"] + " years old.");
        }

       
        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();
    }
}

 
Here, we first use the constructor method DataTable() to create a fresh instance of the DataTable class. We then add two columns named “Name” and “Age” to the DataTable using the Columns.Add() method.

Next, we use the DataTable to add two rows using the Rows.Add() method. Each row represents a person with a name and an age.

Finally, we iterate over each row in the DataTable using a foreach loop and display the person’s name and age using the Console.WriteLine() method.


This output shows the contents of the DataTable we created, with each person’s name and age displayed on a separate line.

Conclusion

The DataTable in C# is a useful object for representing tabular data, because it enables us to manipulate data in a structured fashion and store it. By adding columns and rows to a DataTable, we can create and manage complex data structures that can be used in a variety of applications.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.