hi
I am doing a project in asp.net with c#. Can anybody help me to edit data in the gridview. How can i enable the cells in gridview to modify the data.

Dani AI

Generated

asked how to enable editing in a GridView with C#. provided a VB example that updates the database; the pattern below shows the WebForms way to enable row editing, common pitfalls, and a compact C# skeleton to apply immediately.

To enable editing: add an Edit button (either AutoGenerateEditButton="true" or a CommandField with ShowEditButton="true"), set DataKeyNames to the primary key, and use BoundField or TemplateField for editable columns. Important sequence in event handlers is: set GridView.EditIndex in RowEditing, perform the update in RowUpdating (using the DataKeys and the new values), then set EditIndex = -1 and rebind.

Common causes of “edit not working”: rebinding the GridView on every Page_Load (bind only when !IsPostBack), missing DataKeyNames, or reading values before the update is applied. For RowUpdating, preferred value retrieval methods are e.NewValues for BoundFields or FindControl for TemplateField controls. Always use parameterized commands or a data source; avoid plain concatenation. Note that AddWithValue can cause type/parameterization issues—prefer explicit parameter types or an ORM for new projects.

Troubleshooting checklist:

  • Verify DataKeyNames contains the PK.
  • Ensure BindGrid() runs only when appropriate.
  • Check that fields intended to be editable are not marked ReadOnly.
  • Wrap ADO.NET objects in using blocks and validate inputs server-side.

Example GridView + C# skeleton:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
  OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelingEdit">
  <Columns>
    <asp:BoundField DataField="Id" ReadOnly="True" />
    <asp:BoundField DataField="Name" />
    <asp:CommandField ShowEditButton="True" />
  </Columns>
</asp:GridView>
protected void gv_RowEditing(object s, GridViewEditEventArgs e) { gv.EditIndex = e.NewEditIndex; BindGrid(); }
protected void gv_RowUpdating(object s, GridViewUpdateEventArgs e) {
  var id = gv.DataKeys[e.RowIndex].Value;
  var name = ((TextBox)gv.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 
  // run parameterized update, then:
  gv.EditIndex = -1; BindGrid();
}
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Security
Imports System.Web.SessionState

Partial Class Edit
    Inherits System.Web.UI.Page
    
    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim objCommand As New SqlCommand()
        objConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
        objCommand.CommandText = "UPDATE Company SET CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax WHERE USERNAME=@UserName"
        objCommand.Parameters.AddWithValue("@UserName", User.Identity.Name)
        objCommand.Parameters.AddWithValue("@CustomerID", txtID.Text)
        objCommand.Parameters.AddWithValue("@CompanyName", (txtCompany.Text).Trim)
        objCommand.Parameters.AddWithValue("@ContactName", (txtContact.Text).Trim)
        objCommand.Parameters.AddWithValue("@ContactTitle", (txtContactTitle.Text).Trim)
        objCommand.Parameters.AddWithValue("@Address", (txtAddress.Text).Trim)
        objCommand.Parameters.AddWithValue("@City", (txtCity.Text).Trim)
        objCommand.Parameters.AddWithValue("@Region", (txtRegion.Text).Trim)
        objCommand.Parameters.AddWithValue("@PostalCode", (txtPostalCode.Text).Trim)
        objCommand.Parameters.AddWithValue("@Country", (txtCountry.Text).Trim)
        objCommand.Parameters.AddWithValue("@Phone", (txtPhone.Text).Trim)
        objCommand.Parameters.AddWithValue("@Fax", (txtFax.Text).Trim)
        objCommand.CommandType = CommandType.Text
        objCommand.Connection = objConnection
        objConnection.Open()
        If Session("Logged_IN").Equals("Yes") Then
            Try
                objCommand.ExecuteNonQuery()
                lblErr.Text = "Profile Updated"
            Catch ex As SqlException
                lblErr.Text = ex.Message
            End Try
        Else

        End If
        

    End Sub

   
End Class
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.