Skip to content
Loading
Dynamically Creating Data-table and Binding To Grid-view
Use below code for .aspx.csa page
  1. public SqlConnection con;  
  2.     protected void Page_Load(object sender, EventArgs e)  
  3.     {  
  4.         if (!IsPostBack)  
  5.         {  
  6.             fillRecords();  
  7.         }  
  8.     }  
  9.   
  10.     private DataTable fillRecords()  
  11.     {  
  12.         con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Sqlonnection"].ConnectionString);  
  13.         DataTable mytable = new DataTable();  
  14.         SqlCommand cmd = new SqlCommand("SELECT id,Name,0 as isNew FROM [tbl_addrow]", con);  
  15.         if (con.State == ConnectionState.Closed)  
  16.             con.Open();  
  17.         SqlDataReader dr = cmd.ExecuteReader();  
  18.         mytable.Load(dr);  
  19.         ViewState["Row"] = mytable;  
  20.         GridView1.DataSource = mytable;  
  21.         GridView1.DataBind();  
  22.         return mytable;  
  23.     }  
  24.     public void createnewrow()  
  25.     {  
  26.         DataTable mytable = new DataTable();  
  27.         mytable = (DataTable)ViewState["Row"];  
  28.         DataRow dr = null;  
  29.         dr = mytable.NewRow();  
  30.         dr["Id"] = txt_id.Text;  
  31.         dr["Name"] = txt_name.Text;  
  32.         dr["isNew"] = "1";  
  33.         mytable.Rows.Add(dr);  
  34.         ViewState["Row"] = mytable;  
  35.         GridView1.DataSource = mytable;  
  36.         GridView1.DataBind();  
  37.         txt_id.Text = txt_name.Text = "";  
  38.         btnSaveRecord.Visible = true;  
  39.     }  
  40.   
  41.   
  42.     protected void btnAddRecord_Click(object sender, EventArgs e)  
  43.     {  
  44.         createnewrow();  
  45.     }  
  46.   
  47.     protected void btnSaveRecord_Click(object sender, EventArgs e)  
  48.     {  
  49.         foreach (GridViewRow gvr in GridView1.Rows)  
  50.         {  
  51.             HiddenField hf = (HiddenField)gvr.FindControl("hf_isNew");  
  52.             if (hf.Value == "1")  
  53.             {  
  54.                 con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Sqlonnection"].ConnectionString);  
  55.                 Label lbl_Id = (Label)gvr.FindControl("lbl_id");  
  56.                 Label lbl_Name = (Label)gvr.FindControl("lbl_Name");  
  57.                 if (con.State == ConnectionState.Closed)  
  58.                     con.Open();  
  59.                 SqlCommand com = new SqlCommand("INSERT INTO tbl_addrow(id,Name) VALUES('" + Convert.ToInt32(lbl_Id.Text.ToString()) + "','" + lbl_Name.Text.ToString() + "')", con);  
  60.                 com.ExecuteNonQuery();  
  61.                 con.Close();  
  62.             }  
  63.         }  
  64.         fillRecords();  
  65.         btnSaveRecord.Visible = false;  
  66.     }  
 
 
 It will work surely
  • Hey Pradeepa bro 
    Thank u so  much its working 
  • Hi Pradeep
     
    Thank u , I ll check but its not inserted to table, please check below attacment txt_id AND txt_name is the txt box ids, not a grid-view id i want save Gridview data not for txt box data