Add dynamically virtual records to Gridview in Asp.net


Let's see how to add records to Gridview virtually. In here I am using "DataTable" to define the columns of "Gridview "
              protected void btnAdd_Click(object sender, EventArgs e)
              { 

                DataTable dt = new DataTable();
                dt.Columns.Add(" ", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Address", typeof(string));
                dt.Columns.Add("Gender", typeof(string));
             
                dt.Columns[0].AutoIncrement = true;
                dt.Columns[0].AutoIncrementSeed = 1;
                } 

 Now, I have already added columns into DataTable. In this step, I am going to add records to DataRows from Text Boxes.
protected void btnAdd_Click(object sender, EventArgs e)
           {  
                ........................................
                DataRow dr = dt.NewRow();
                dr["Name"] = this.txtName.Text.ToString();
                dr["Address"] =this.txtAddress.Text.ToString();
                dr["Gender"] = this.txtGender.Text.ToString();
                dt.Rows.Add(dr);
           }
 Now, I have filed DataRows in DataTable. From here, I am going to bind all records in DataTable to Gridview.(don't make "false" Auto generate columns property  of the Gridview)
        protected void btnAdd_Click(object sender, EventArgs e)
         {  
                ......................
                gdView.DataSource = dt;
                gdView.DataBind();
         }
 I have already bound DataTable to Gridview but when we click the add button second time, it will insert new record without previous record the reason is page post back which will blank the controls of the page. to solve the problem, we can use "ViewState or SessionState" and here I am goin to use "ViewState" because I don't want to keep these records in different page.
 Here is final code:
      protected void btnAdd_Click(object sender, EventArgs e)
       {
  DataTable dt = new DataTable();
          if (ViewState["GrdData"] == null)
            {
                dt.Columns.Add(" ", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Address", typeof(string));
                dt.Columns.Add("Gender", typeof(string));
               
                dt.Columns[0].AutoIncrement = true;   
                dt.Columns[0].AutoIncrementSeed = 1;
            }
            else
              dt = (DataTable)ViewState["GrdData"] ;  
                DataRow dr = dt.NewRow();
                dr["Name"] = this.txtName.Text.ToString();
                dr["Address"] =this.txtAddress.Text.ToString();
                dr["Gender"] = this.txtGender.Text.ToString();
                dt.Rows.Add(dr);
                gdView.DataSource = dt;
                 gdView.DataBind();
                 ViewState["GrdData"] = dt;
     }
If you have any doubts of this code, watch my video here is the link below 
Enjoy this...!
Happy coding...!

No comments:

Post a Comment