Hello All,


How I can edit rows of gridview using Command Field ‘Edit,Update,Cancel’ of the gridview?

I bind the gridview with the Session variable, which contain and returns the DataTable, the coding is as follows:-

protected void Page_Load(object sender, EventArgs e)
{
        GridView1.DataSource = (DataTable)Session["MyShoppingCart"];
        GridView1.DataBind();
}

One more thing is that, on the edit event user can only update ‘Quantity’ column of the gridview.

Anyone please solve this problem.

Thanks.

Recommended Answers

All 2 Replies

Hello
write the following code & function to GridViewevent

public void loadTaxDataToGrid()
        {
             // feach the record from database and bind to gridview1
         }
 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            loadDataToGrid();
        }
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            loadDataToGrid();
        }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                  string "DataKeyNames"= (string)GridView1.DataKeys[e.RowIndex].Value;
                 TextBox "Control Name from GridView1" = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Control Name from GridView1");

//// Update that Record for that write store procedure
               
                            GridView1.EditIndex = -1;
                            loadDataToGrid();
                        }
                       
               
            }
            catch (Exception ex)
            {
                lblmsg.Text = ex.Message;
            }
        }

bye....

hi,

you can check this example for editing and deleting rows in gridview

<asp:GridView ID="gdview"  runat="server" ShowFooter="true" AutoGenerateColumns="False"  DataKeyNames="CategoryID"  OnRowCancelingEdit="gdview_RowCancelingEdit" OnRowDeleting="gdview_RowDeleting" OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating" Width="100%" AllowPaging="True" PageSize="5" OnPageIndexChanging="gdview_PageIndexChanging" OnRowDataBound="gdview_RowDataBound"  >
        <Columns>
            <asp:BoundField HeaderText="Category Name" DataField="CategoryName" SortExpression="CategoryName" >
                <ItemStyle Height="20px" Width="150px" />
            </asp:BoundField>
            
            <asp:CommandField ShowEditButton="True">
                <ItemStyle Width="100px" />
            </asp:CommandField>
            <asp:TemplateField>
            
          
          
            <ItemTemplate>
            <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
            
            </ItemTemplate>
            <ItemStyle Width="100px" />
        
            </asp:TemplateField>
        </Columns>
 

 </asp:GridView>

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            bindgrid();
            total = 0;
           
        }

    }

    public void bindgrid()
    {
        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Categories ", conn);

        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        gdview.DataSource = ds.Tables[0].DefaultView;
        gdview.DataBind();

      
    }
    protected void gdview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        gdview.EditIndex = e.NewEditIndex;
      
        bindgrid();
     
    }
    protected void gdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
        
        Image img = new Image();
       
        int catid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString());
        string strcatname=((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.UpdateCommand = new SqlCommand("update Categories set CategoryName='" + strcatname + "' where CategoryID=" + catid, conn);
        da.UpdateCommand.ExecuteNonQuery();
        conn.Close();
        gdview.EditIndex = -1;
        bindgrid();
  
       

    }
    protected void gdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdview.EditIndex = -1;
        bindgrid();
    }
    protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       
        int catid = int.Parse(gdview.DataKeys[0].Value.ToString());
       SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.DeleteCommand = new SqlCommand("delete from Categories where CategoryID="+catid, conn);
        da.DeleteCommand.ExecuteNonQuery();
        conn.Close();
        bindgrid();
    }

      protected void gdview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdview.PageIndex = e.NewPageIndex;
        bindgrid();
        
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.