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
hi

use this

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


refer links
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0
http://forums.asp.net/p/1255252/2331422.aspx
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
(!isPostBack)
{
//urGridViewDataBind()
}

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

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.

Thanks,

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">
  <EditItemTemplate>
    <asp:TextBox Text='<%# Eval("name") %>' ID="txtname" runat="server"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
    <asp:Label ID="lblName" runat="Server" Text='<%# Eval("name") %>' />
  </ItemTemplate>
</asp:TemplateField>
► 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;
  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 table1 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;
  //BindGrid();
 
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
  GridView1.EditIndex = -1;
  //BindGrid();
    
}