Edit and Update records of Gridview

In this post I am trying to edit and update the records of the gridview.

First make sure edit button of the grideview is true and then write the code inside the "RowEditing" event of the grid.
protected void gdView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdView.EditIndex = e.NewEditIndex;

            DataTable dt = (DataTable)ViewState["GrdData"];

            gdView.DataSource = dt;
            gdView.DataBind();
            ViewState["GrdData"] = dt;

        }
if you run and click edit button, you can see that the row of  gridview is in edit mode displaying update and cancel button on it.

In this step, it should implement the code for update button, so that save the changes in viewstate. Write the code inside the "RowUpdating" event of the grid
protected void gdView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable dt = ViewState["GrdData"] as DataTable;
            GridViewRow row = gdView.Rows[e.RowIndex];
           dt.Rows[row.DataItemIndex]["Name"] = ((TextBox)                                  (row.Cells[1].Controls[0])).Text;
          dt.Rows[row.DataItemIndex]["Address"] = ((TextBox)                                  (row.Cells[2].Controls[0])).Text;
               dt.Rows[row.DataItemIndex]["Gender"] = ((TextBox)                                             (row.Cells[3].Controls[0])).Text;
            gdView.EditIndex = -1;
            gdView.DataSource = dt;
            gdView.DataBind();
            ViewState["GrdData"] = dt;

        }
If you have any doubts, watch my video here is the link
http://youtu.be/OfLRC8w21FU

Enjoy this...!
Happy coding...!

Delete records from Gridview


In the last post, I have implemented the cods how you can add the records to gridview using ViewState, DataTable and DataRow. Now, I am trying to delete the records one by one which I added at last.

At first, make "true" delete button of the gridview and write the code in the "RowDeleting" event
(this code 100% work)

protected void gdView_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

            if (ViewState["GrdData"] != null)

            {

                dt = ViewState["GrdData"] as DataTable;

                dt.Rows.RemoveAt(e.RowIndex);

                gdView.DataSource = dt;

                gdView.DataBind();

                ViewState["GrdData"] = dt;

            }

        }
If you have doubts, watch my video, to make sure the cods here is the link 
http://youtu.be/xt_vz3Lp5Mc



Enjoy this....!
Happy coding.....!

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...!