Monday, March 12, 2012

How to handle GridView Event Handler of Child GridView

Hi All,

I am developing a website which needs nested gridview control. All things are fine upto binding data to childgrid, but i am not able to handle Event Handler of Edit, Update, Delete, Cancel for Child Grid View.

I am binding the data to child gridview at RowDataBound Event of Parent GridView.


Here is the code for the same :

protected void gridID_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
childgrid = (System.Web.UI.WebControls.GridView)e.Row.Cells[2].FindControl("childgrid");

string comID = gridID.DataKeys[e.Row.DataItemIndex].Value.ToString();
con.Open();

cmd = new SqlCommand("SELECT * FROM Pipeline where CompName ='" + comID.ToString() + "'", con);

childgrid.DataSource = cmd.ExecuteReader();
childgrid.DataBind();
con.Close();
}

}

But when I click Edit Button of Child Grid View, it's not able to open selected row in edit mode. but when we hide child grid and open once again it is opening in edit mode.

Could you please help me with this?


Thanks,


Jay

Hi:

Could you post your aspx? Did you put it in <EditItemTemplate>?

Thanks

Labels: , , , , , , , , , , , , ,

How to Handle dynamic Button Click event

Dear sir,

In the above code, I am binding the data such as description of the product, and i have added one button. When I click on the button for the particular row, that row's details has to be added in a separate datatable.


<asp:DataGrid ID="dgGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn >
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Add To Cart" Text="Add To Cart" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Please let me know how to proceed.

Thanks in Advance,

Regards,

Arun.

How about something like this:

protected void dgGrid_ItemCommand(object source, DataGridCommandEventArgs e){Button Button1 = e.CommandSourceas Button;if (Button1 ==null) {return; }DataGridItem dgi = Button1.NamingContaineras DataGridItem;if (dgi ==null) {return; }Label Label1 = dgi.FindControl("Label1")as Label;if (Label1 ==null) {return; }string description = Label1.Text;// Do whatever you want here}

Labels: , , , , , , , , , , , ,