ASP.NET - Gridview edit - Asked By msakt on 11-May-12 01:54 AM

hi,in my grid have pagesize 15,hve 109 record bind my grid..if click edit button its work current row modify for edit..evan click any page...

but my problem is...here i use search button and textbox..if i enter word in textbox and serch show record sililar that word..for example show 10 records in grid  if i click edit in first row,edit mode modify in first row of page one..because if click edit buuton show all records
Neha Garg replied to msakt on 11-May-12 02:34 AM
Hi Msakt,

See the below example:

<asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False"
  OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4"
  ForeColor="#333333" GridLines="None">
  <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
  <Columns>
    <asp:TemplateField>
      <HeaderTemplate>
        <asp:Label ID="lblPID" runat="server" Text="Product ID"></asp:Label>
      </HeaderTemplate>
      <ItemTemplate>
        <asp:Label ID="lblProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="txtProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <HeaderTemplate>
        <asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
      </HeaderTemplate>
      <ItemTemplate>
        <asp:Label ID="lblProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
  </Columns>
  <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
  <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  <AlternatingRowStyle BackColor="White" />
</asp:GridView>


aspx.cs

protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
  {
    gvProducts.EditIndex = e.NewEditIndex;
  }
 
  protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
 
    int i = e.RowIndex;
    object control = gvProducts.Rows[i].FindControl("txtProdID");
    //i want to access the new value from the object "control" but i m getting the previous value only
  }
dipa ahuja replied to msakt on 11-May-12 03:38 AM
To Update Records Follow :
 
In aspx page add :
 
<asp:CommandField ButtonType="Button" ShowEditButton="True" ShowSelectButton="True" />
 
and implement the gridView's RowEditing , RowUpdating and RowCancelingEdit Events as :
 
 
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
    GridView1.EditIndex = e.NewEditIndex;
    /*Code to BindGrid */
  }
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
    int id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text);//ID
    string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname")).Text;
 
 
    SqlConnection connect = new SqlConnection("ConnString");
    connect.Open();
 
    string q = "Update people set name=@name where id=@id";
 
    SqlCommand comm = new SqlCommand(q, connect);
    comm.Parameters.AddWithValue("name", name);
    comm.Parameters.AddWithValue("id", id);
    comm.ExecuteNonQuery();
    connect.Close();
 
    GridView1.EditIndex = -1;
 
    /*Code to BindGrid */
  }
  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    GridView1.EditIndex = -1;
    /*Code to BindGrid */
 
  }
 
 
 
Somesh Yadav replied to msakt on 11-May-12 08:00 AM
try this,
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0