---
title: "Tutorial To Use DataTable in .Net."  
description: "When I have joined IT company then it is very easy to deal with console application in C# but for the form application it is new concept for me.  I di"  
author: "Abhishek Srivasatava"  
published: 2016-11-03  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11222/tutorial-to-use-datatable-in-dot-net  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# Tutorial To Use DataTable in .Net.

When I have joined IT company then it is very easy to deal with [console application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) in C# but for the [form application](https://www.mindstick.com/forum/34200/system-data-sqlclient-sqlexception-occurred-while-using-checked-or-radio-button-in-form-application) it is new concept for me. I didn't have much knowledge about the in-build classes of form application. There are many classes like DataTable, SqlDataAdapter etc.\

So, I am explaining about the DataTable. It is used to [create table](https://www.mindstick.com/articles/443/how-to-create-table-in-sql-server) in the form application.

Just think about the [table format](https://www.mindstick.com/forum/1021/table-format-in-sencha-touch-2) of the [excel sheet](https://www.mindstick.com/forum/23040/to-import-excel-sheet-into-sql-database) and relate it with form application. Although it is basic to understand the DataTable but it will clear the concept how to [bind data](https://www.mindstick.com/forum/415/how-to-bind-data-in-windows-phone-7-using-wcf-service) with SQL. Let’s suppose you have to create a table Student details in which you required 4 columns Roll Number, [Student Name](https://www.mindstick.com/forum/157178/how-to-filter-1000-of-student-name-in-php-in-ascending-order), Father Name and Mother Name.

**Below are the steps you need to follow.**

**Step 1****:** How to give the name of the table?

Syntax for giving the name of the table is below:

DataTable St_table =new DataTable (“Student Details”).\

**Step 2:** Creating the column, DataColumn class is used in this regard.\

**Syntax :** DataColumn Stcolumn;

**Step 3:** Constraint can be added to the column means that what type of data can be accessed by the column.\

```
Stcolumn = new ColumnData();Stcolumn.DataType= GetType (“Int32”); Stcolumn.Name= “Roll Number”;Stcolumn.Readonly = true;Stcoumn.unque=true;
```

**Step 4:** Adding new column to the table.

```
St_table.Columns.Add(Stcolumn)
```

Step 5, Step 6 & step 7: repeat step 3 and 4 for the column student name, father name and mother name.

**Step 8:** Giving value to the column means adding the row for the table.

RowData is the class which will be used to [add rows](https://www.mindstick.com/forum/364/how-can-i-add-rows-in-datafridview-in-c-sharp).

```
Example DataRow stRows;             stRows = St_table.NewRow();            stRows["Roll number"]= 1001;            stRows["Student Name"] = "Abhi";            stRows["Father Name"] = "Ab";            stRows["Mother Name"] = "a";                        St_table.Rows.Add(stRows); 
```

**Here is the complete code for the above example.**

```
public void filldata()        {            System.Data.DataTable St_table = new DataTable("Student details");            DataColumn Stcolumn;            Stcolumn = new DataColumn();            Stcolumn.DataType = System.Type.GetType("System.Int32");            Stcolumn.ColumnName = "Roll number";            Stcolumn.ReadOnly = true;            Stcolumn.Unique = true;            St_table.Columns.Add(Stcolumn);             Stcolumn = new DataColumn();            Stcolumn.DataType = System.Type.GetType("System.String");            Stcolumn.ColumnName = "Student Name";            Stcolumn.ReadOnly = true;            Stcolumn.Unique = true;            St_table.Columns.Add(Stcolumn);             Stcolumn = new DataColumn();            Stcolumn.DataType = System.Type.GetType("System.String");            Stcolumn.ColumnName = "Father Name";            Stcolumn.ReadOnly = true;            Stcolumn.Unique = true;            St_table.Columns.Add(Stcolumn);             Stcolumn = new DataColumn();            Stcolumn.DataType = System.Type.GetType("System.String");            Stcolumn.ColumnName = "Mother Name";            Stcolumn.ReadOnly = true;            Stcolumn.Unique = true;            St_table.Columns.Add(Stcolumn);             DataRow stRows;             stRows = St_table.NewRow();            stRows["Roll number"]= 1001;            stRows["Student Name"]= "Abhi";            stRows["Father Name"]= "Ab";            stRows["Mother Name"]= "a";            St_table.Rows.Add(stRows);            dataGridView1.DataSource =St_table;        }
```

**\
Now, it will very easy how to understand how to get data using [SQL database](https://www.mindstick.com/articles/337535/connecting-to-sql-databases-ado-dot-net-essentials):\
\**

```
SqlConnection con3 = new SqlConnection();            con3.ConnectionString = (@"Data Source=msclient-012\sqlexpress;Initial Catalog=testting_st;User ID=sa;Password=mindstick");
SqlCommand cmd = new SqlCommand("select * from student_details", con);            SqlDataReader sdr =  cmd.ExecuteReader();            DataTable dt=new DataTable();            dt.Load(sdr);            dataGridView1.DataSource = dt;
```

---

Original Source: https://www.mindstick.com/blog/11222/tutorial-to-use-datatable-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
