ASP.NET - gridviews prolem..? - Asked By sunil pandey on 06-Feb-12 04:50 AM

Hi all

i am inserting some data.and make validation and below it i taken gridviews .when iam editing the gridviews event is not
fired and required field validation come in action .i also taken update panel but same issue
smr replied to sunil pandey on 06-Feb-12 04:54 AM

use this

protected void grdContact_RowEditing(object sender, GridViewEditEventArgs e)
    grdContact.EditIndex = e.NewEditIndex;

refer links
D Company replied to sunil pandey on 06-Feb-12 05:27 AM
Hello friend,

First of all always make use a habit of putting break point on the code segment, and see what it returns, and check if the event if gets fired or not.generally this problem occurs  when DataBind is called before the control event is firing.

If you call DataBind in Page_Load than do like this

Hope it helps!!
kalpana aparnathi replied to sunil pandey on 06-Feb-12 05:38 AM

Do you use inheritance this point with master page or any page ?if yes then remove inheritance concept because something in inheritance stack didn't worked.


dipa ahuja replied to sunil pandey on 06-Feb-12 06:15 AM
► Add the EditItemTemplate on each table field which you want to update at runtime for ex:
<asp:TemplateField HeaderText="Name">
    <asp:TextBox Text='<%# Eval("name") %>' ID="txtname" runat="server"></asp:TextBox>
    <asp:Label ID="lblName" runat="Server" Text='<%# Eval("name") %>' />
► Next Add add commandField in your gridview:
<asp:CommandField ButtonType="Button"
ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
► Now Implement the events rowEditing, RowUpdating and rowCancelEdit to edit the gridview:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  GridView1.EditIndex = e.NewEditIndex;
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);
  string q = "Update table1 set name=@name where id=@id";
  SqlCommand comm = new SqlCommand(q, connect);
  comm.Parameters.AddWithValue("name", name);  
  comm.Parameters.AddWithValue("id", id);
  GridView1.EditIndex = -1;
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  GridView1.EditIndex = -1;