ASP.Net Gridview RowCommand Edit, Update

2.4k Views Asked by At

I am working on asp.net gridview on webforms and using Gridview RowCommand method of GridView OnRowCommand event for the Buttons View, Edit & Update inside TemplateField.

if (e.CommandName == "EditContract") 
  {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
            gvContract.SelectRow(rowIndex);

            using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@RoleName", SqlDbType.Char).Value = Session["RoleName"];
                cmd.Parameters.Add("@UnitName", SqlDbType.Char).Value = Session["UnitName"];
                cmd.Parameters.Add("@ContrSerialNo", SqlDbType.Int).Value = SerialNo;

                dAdapter = new SqlDataAdapter(cmd);

                DataTable DtContract = new DataTable();
                dAdapter.Fill(DtContract);
     }

if (e.CommandName == "UpdateContract") 
           {

            lblMessage.Text = "";
            lblFile.Text = "";

            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

             }
          } 

This code is working fine when the page loads first time but has 2 problems after that. i.e. 1. It is editing and updating data when the page gets load for the first time but when I try to edit the same row or any other then it doesn't. I know it is because I have defined the !Page.IsPostback method on the Page_Load event but I do not know how to tackle this situation. 2. Problem is How to restrict the gridview row to update only that data whose row is selected?

Please suggest me a solution.

Gridview Sample

1

There are 1 best solutions below

4
Priom Sarkar On

First of all, you should bind the Gridview in UpdateContract.Like below

if (e.CommandName == "UpdateContract") {

            lblMessage.Text = "";
            lblFile.Text = "";

            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

***You need to bind Gridview here then it will not have to go for postback every time ***
                Gridview.datasource= datatable;
                Gridview.Databind();
}

You can use updatepanel for update only the needed element and avoid refresh.

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>


    ***Here put your gridview***

   <asp:gridview></asp:gridview>
   </ContentTemplate>
   </asp:UpdatePanel>