How to create DataTable in C# and how to Bind Data to DataTable?
In this article you will learn how to create DataTable with multiple columns and how to bind Data to DataTable. You will also know how to Bind Datatable to DataGridView with Only some columns in DataTable, how to set width for DataGridView column and how to specify which column in datatable needs to bind for which column in datagridview.
We are here going to learn how to create DataTable in C#.
Declare an object of type DataTable. To use DataTable, DataColumn, DataSet we have to import System.Data Namespace
Now lets see how to create columns in the datatable. While creating the column we have to provide column name and datatype of value we are storing in that.
In the above method we can directly add multiple columns. We can also add single column like this:
DataColumn Objcol=new DataColumn();
Objcol.ColumnName="sno";
Objcol.DataType=typeof(Int16);
DtExample.Columns.Add(Objcol);How to bind Data to C# DataTable
In the above method we created DataTable DtExample. Now we can add Data to this table using for each loop. Generally in our application we had three layers to interact with database.Iin the same i got datatable from database and i want to add serial no column and then bind data to C# DataTable. We can write that condition in following manner.
DataTable dt=BusincessAccessLayer.common.Getstudentdata().Tables[0];//this method return DataSet
foreach (DataRow r in dt.Rows)
{
DataRow dr = DtExample.NewRow();
dr[0] = sno++;
dr[1] = r.ItemArray[0];
dr[2] = r.ItemArray[1];
dr[3] = r.ItemArray[2];
DtExample.Rows.Add(dr);
}
We can add manually data to datatable in following manner using DataRow. create datarow and add data to that row and bind that row to C# DataTable.
Based on datatype of column we have to add data otherwise it will return error while runtime.Here for adding values to row we can use either index or column name.
DataRow ObjDr=new DataRow();
ObjDr[0]="DataRow1";
ObjDr[1] = false; //Based on datatype of column we have to add data.
ObjDr["StudentName"]="Sekhar Babu";
DtExample.Rows.Add(ObjDr);
Binding DataTable to DataGridView
We can directly bind datatable as datasource to DataGridView as follows:
DataGridView1.DataSource=DtExample.DefaultView;// all the columns in datatable with all rows of data is directly added
Some times we have to show only two or three columns and datagrid click event we have to put other columns data in some controls in form. In that context we will directly make a particular column visible false and at the same we can also set the width for columns in DataGridview.
DataGridView1.DataSource =DtExample.DefaultView;
DataGridView1.Columns[0].DataPropertyName = "Sno";
DataGridView1.Columns[1].DataPropertyName = "Studentid";
DataGridView1.Columns[2].DataPropertyName = "StudentName";
DataGridView1.Columns[3].DataPropertyName = "Branch";
DataGridView1.Columns[0].Width = 50;
DataGridView1.Columns[1].Width = 100;//setting width for column
DataGridView1.Columns[2].Width = 130;
DataGridView1.Columns[3].Width = 130;
DataGridView1.Columns[3].Visible=false;//Making one column visible false.
i can demo to set
you can more record insert to use loop condition to your requirement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet _ds = new DataSet();
DataTable _dt = new DataTable();
DataRow _dr;
try
{
_dt.Columns.Add(new DataColumn("srno", typeof(int)));
_dt.Columns.Add(new DataColumn("Name", typeof(string)));
_dt.Columns.Add(new DataColumn("Address", typeof(string)));
_dt.Columns.Add(new DataColumn("MobileNo", typeof(int)));
_dt.Columns.Add(new DataColumn("EmailAddress", typeof(string)));
_dr = _dt.NewRow();
_dr[0] = 1;
_dr[1] = "Name1";
_dr[2] = "Address1";
_dr[3] = 93221489;
_dr[4] = "EmailAddress1";
_dt.Rows.Add(_dr);
_ds.Tables.Add(_dt);
GridView1.DataSource = _ds;
GridView1.DataBind();
}
catch(Exception ex)
{
}
}
}