blog

Home / DeveloperSection / Blogs / Tutorial To Use DataTable in .Net.

Tutorial To Use DataTable in .Net.

Abhishek Srivasatava 2252 03-Nov-2016

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 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 in the form application.

Just think about the table format of the excel sheet and relate it with form application. Although it is basic to understand the DataTable but it will clear the concept how to bind data with SQL. Let’s suppose you have to create a table Student details in which you required 4 columns Roll Number, Student Name, 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.

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 = newDataTable("Student details");
            DataColumn Stcolumn;
            Stcolumn = newDataColumn();
            Stcolumn.DataType = System.Type.GetType("System.Int32");
            Stcolumn.ColumnName = "Roll number";
            Stcolumn.ReadOnly = true;
            Stcolumn.Unique = true;
            St_table.Columns.Add(Stcolumn);
 
            Stcolumn = newDataColumn();
            Stcolumn.DataType = System.Type.GetType("System.String");
            Stcolumn.ColumnName = "Student Name";
            Stcolumn.ReadOnly = true;
            Stcolumn.Unique = true;
            St_table.Columns.Add(Stcolumn);
 
            Stcolumn = newDataColumn();
            Stcolumn.DataType = System.Type.GetType("System.String");
            Stcolumn.ColumnName = "Father Name";
            Stcolumn.ReadOnly = true;
            Stcolumn.Unique = true;
            St_table.Columns.Add(Stcolumn);
 
            Stcolumn = newDataColumn();
            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:

SqlConnection con3 = newSqlConnection();
            con3.ConnectionString = (@"Data Source=msclient-012\sqlexpress;Initial Catalog=testting_st;User ID=sa;Password=mindstick");
SqlCommand cmd = newSqlCommand("select * from student_details", con);
            SqlDataReader sdr =  cmd.ExecuteReader();
            DataTable dt=newDataTable();
            dt.Load(sdr);
            dataGridView1.DataSource = dt;


Updated 16-Mar-2018

Leave Comment

Comments

Liked By