Tuesday, October 21, 2014

how to create a table dynamically and bind to gridview in c#

Posted by Gnani Palepu  |  No comments


 if you want more related search please click here  
 please help to other  
 http://scriptquery.blogspot.in/  

 how to create a table dynamically and bind to gridview in c#  
 -------------------------------------------------------------  
 step1):  
 -------  
  <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
 <title>Dynamically create datatable and bind data to it in asp.net</title>  
 </head>  
 <body>  
 <form id="form1" runat="server">  
 <div>  
 <asp:GridView ID="gvDetails" runat="server">  
 </asp:GridView>  
 </div>  
 </form>  
 </body>  
 </html>  
 step 2):  
 -------  
 using System;  
 using System.Data;  
 step 3):  
 ---------  
  protected void Page_Load(object sender, EventArgs e)  
 {  
 if(!IsPostBack)  
 {  
 BindGridviewData();  
 }  
 }  
 /// <summary>  
 /// Dynamically create & bind data to datatable and bind datatable to gridview  
 /// </summary>  
 protected void BindGridviewData()  
 {  
 DataTable dt = new DataTable();  
 dt.Columns.Add("UserId", typeof(Int32));  
 dt.Columns.Add("UserName", typeof (string));  
 dt.Columns.Add("Education", typeof (string));  
 dt.Columns.Add("Location",typeof(string));  
 DataRow dtrow = dt.NewRow();  // Create New Row  
 dtrow["UserId"] = 1;      //Bind Data to Columns  
 dtrow["UserName"] = "Gnani";  
 dtrow["Education"] = "B.Tech";  
 dtrow["Location"] = "Prakasam";  
 dt.Rows.Add(dtrow);  
 dtrow = dt.NewRow();        // Create New Row  
 dtrow["UserId"] = 2;        //Bind Data to Columns  
 dtrow["UserName"] = "gopal";  
 dtrow["Education"] = "M.tech";  
 dtrow["Location"] = "srikakulam";  
 dt.Rows.Add(dtrow);  
 dtrow = dt.NewRow();       // Create New Row  
 dtrow["UserId"] = 3;       //Bind Data to Columns  
 dtrow["UserName"] = "chandrababu";  
 dtrow["Education"] = "B.Tech";  
 dtrow["Location"] = "gutur";  
 dt.Rows.Add(dtrow);  
 dtrow = dt.NewRow();       // Create New Row  
 dtrow["UserId"] = 4;       //Bind Data to Columns  
 dtrow["UserName"] = "arun";  
 dtrow["Education"] = "CA";  
 dtrow["Location"] = "Guntur";  
 dt.Rows.Add(dtrow);  
 gvDetails.DataSource = dt;  
 gvDetails.DataBind();  
 }  

 if you want more related search please click here  
 please help to other  
 http://scriptquery.blogspot.in/  


7:25 AM Share:

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
Proudly Powered by Blogger.
back to top