Click here to Skip to main content
16,019,140 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code
XML
<asp:GridView ID="HeaderGV" runat="server" AutoGenerateColumns="False" OnRowDataBound="HeaderGV_RowDataBound"
                                  Width="100%" OnSelectedIndexChanged="HeaderGV_SelectedIndexChanged" HeaderStyle-CssClass="grid_col_head">
                                  <Columns>
                                      <asp:BoundField DataField="SNO" HeaderText="SNO" ReadOnly="true" ItemStyle-Width="15">
                                          <ItemStyle Width="15px" />
                                      </asp:BoundField>
                                      <asp:TemplateField HeaderText="Header">
                                          <EditItemTemplate>
                                              <%--<asp:TextBox ID="HeaderTxt"  runat="server"></asp:TextBox>--%>
                                              <asp:Label ID="HeaderLbl" runat="server" Text='<%#Bind("PARAM_HEAD_NAME") %>'></asp:Label>
                                          </EditItemTemplate>
                                          <ItemTemplate>
                                              <asp:Label ID="HeaderLbl" runat="server" Text='<%#Eval("PARAM_HEAD_NAME") %>'></asp:Label>
                                              <asp:GridView ID="ParamGV" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="ParamGV_SelectedIndexChanged"
                                                  OnRowDataBound="ParamGV_RowDataBound" Width="100%" HeaderStyle-CssClass="grid_col_head">
                                                  <Columns>
                                                      <asp:BoundField DataField="SNO" ItemStyle-Width="15">
                                                          <ItemStyle Width="15px" />
                                                      </asp:BoundField>
                                                      <asp:BoundField DataField="PARAM_HEAD_ID" />
                                                      <asp:BoundField DataField="PARAM_SUB_ID" />
                                                      <asp:BoundField DataField="PARAM_SUB_NAME" />
                                                      <asp:CommandField SelectText="EDIT" ShowSelectButton="True" ItemStyle-Width="15"
                                                          HeaderText="EDIT">
                                                          <ItemStyle Width="15px" />
                                                      </asp:CommandField>
                                                  </Columns>
                                              </asp:GridView>
                                          </ItemTemplate>
                                      </asp:TemplateField>
                                      <asp:BoundField DataField="PARAM_HEAD_ID" HeaderText="HeaderID" ReadOnly="true" />
                                      <asp:CommandField HeaderText="EDIT" ItemStyle-Width="15" SelectText="EDIT" ShowSelectButton="True">
                                          <ItemStyle Width="15px" />
                                      </asp:CommandField>
                                  </Columns>
                                  <HeaderStyle CssClass="grid_col_head" />
                              </asp:GridView>




MSIL
protected void ParamGV_SelectedIndexChanged(object sender, EventArgs e)
       {
           try
           {


               GridViewRow row = HeaderGV.SelectedRow;
               int rowindex= HeaderGV.SelectedIndex;

               GridView gv = this.HeaderGV;
               GridView gv1 = (GridView)this.HeaderGV.Rows[rowindex+1].FindControl("ParamGV");



           GridViewRow Childrow = gv1.SelectedRow;

           int RowIndex = gv1.SelectedIndex;
           HeadNameDdl.SelectedValue = Server.HtmlDecode(Childrow.Cells[1].Text).Trim();
           ViewState["ParameterId"] = Server.HtmlDecode(Childrow.Cells[2].Text).Trim();
           ParameterNametxt.Text = Server.HtmlDecode(Childrow.Cells[3].Text).Trim();


           }
           catch (Exception ex)
           {
               Literal li = new Literal();
               li.Text = "<script> alert('" + ex.Message + "');</script>";
               Page.Controls.Add(li);
           }
       }





in my code HeaderGV(master) ParamGV (child)

In itemTemplate of master grid i placed a child grid
when i want to fire selectedIndexchanged event for child grid am unable to get mastert grid row
Posted

1 solution

The basic technique on nested items within an ASP.NET Data Control
item template is:

  1. Subscribe OnItemDataBound event for a parent grid.
  2. Find the nested controm within the event handler funcntion :
    DataGrid ParamGV= (DataGrid)e.Item.FindControl("ParamGV ");
  3. Once you have this nested grid in your function you can do anything
    you want with it: subscribe for events, assign parameters, etc..


PS. code may slightly vary for different data controls. I am more familiar with DataView or Repeater, but the pattern is the same
 
Share this answer
 
Comments
karthikkushala 12-Oct-10 0:26am    
Thank q for replying i will try it
karthikkushala 12-Oct-10 0:47am    
protected void HeaderGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (e.Row.RowType != DataControlRowType.Pager)
{
e.Row.Cells[2].Visible = false;
if (Session["Type"].ToString() == "Parameter")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Cells[1].ForeColor = System.Drawing.Color.FromArgb(153, 0, 0);
e.Row.Cells[1].BackColor = Color.FromArgb(221, 221, 221);
//e.Row.Cells[1].Font.Bold = true;
e.Row.Cells[1].Font.Size = 12;
e.Row.Cells[1].Font.Name = "Century";
//e.Row.Attributes.Add("onmouseover", "this.className='highlightrow'");
//e.Row.Attributes.Add("onmouseout", "this.className='normalrow'");
//this is line of code because of ur suggestion
GridView Par = (GridView)e.Row.FindControl("ParamGV");



}
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
}
e.Row.Cells[0].Visible = false;
e.Row.Cells[3].Visible = false;




}


}

if (row.DataItem == null)
{
return;
}


GridView gv = new GridView();
objdc.HeaderID = Convert.ToInt32(row.Cells[2].Text);
gv = (GridView)row.Cells[1].FindControl("ParamGV");
DataTable dt = new DataTable();
if (gv != null && Session["Type"].ToString() == "Parameter")
{
dt = objbl.FillGridForFormParameters(ref objdc);
gv.DataSource = dt;
gv.DataBind();
}
else
{
//gv.DataSource = null;
//gv.DataBind();
}



}

sir but i have to write selectindexchage for my child grid
when i wrote GridView Par = (GridView)e.Row.FindControl("ParamGV"); in RowDataBound event how would i call ParamGV_selectedIndexChanges
Ed Guzman 12-Oct-10 14:11pm    
GridView Par = (GridView)e.Row.FindControl("ParamGV");
Par.SelectedIndexChanged += new EventHandler(ParamGV_SelectedIndexChanged);

void ParamGV_SelectedIndexChanged(object sender, EventArgs e)
{
}
karthikkushala 20-Oct-10 6:02am    
protected void ParamGV_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SaveImgBtn.Visible = false;
UpdateImgBtn.Visible = true;
HeadingRdBtn.Enabled = false;
ParameterRdBtn.Enabled = false;
FormTypeDdl.Enabled = false;
HeadNameDdl.Enabled = false;



GridViewRow row = HeaderGV.SelectedRow;
int rowindex = HeaderGV.SelectedIndex;

GridView gv = this.HeaderGV;
GridView gv1 = (GridView)this.HeaderGV.Rows[rowindex + 1].FindControl("ParamGV");



GridViewRow Childrow = gv1.SelectedRow;

int RowIndex = gv1.SelectedIndex;
HeadNameDdl.SelectedValue = Server.HtmlDecode(Childrow.Cells[1].Text).Trim();
ViewState["ParameterId"] = Server.HtmlDecode(Childrow.Cells[2].Text).Trim();
ParameterNametxt.Text = Server.HtmlDecode(Childrow.Cells[3].Text).Trim();


}

here am getting headerGV selected row -1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900