Wednesday, March 28, 2012

Datatable to Gridview in asp.net

Title: How to bind the data to grid view using data table in asp.net c#.net

Description:
Here i will show how to transfer the data the from data table to Grid view .For this i have generated custom data table " dtOrders" and add desired columns to it.Then i created a object to add the row data into data table and expression property is used to get the desirable format of data.

Example:
DataTable dtOrder = new DataTable();
dtOrder.Columns.Add("OrderId"); 
dtOrder.Columns.Add("Name");  
dtOrder.Columns.Add("Quantity");  
dtOrder.Rows.Add(new Object[] { "12", "pharmacy", "2000" });
dtOrder.Columns[3].Expression = string.Format("{0},{1},{2}", "OrderId", "Name", "Quantity"); 
gvOrders.DataSource = dtOrders;    
gvOrders.DataBind();

Post a Comment