Question : Nested Gridviews RowDataBound Event Problem

Hello, my problem is with nested gridviews. i have one gridview(name "GrdMain") and another one(named "GridSub") is inside of that gridview's template field! i can not reach the variables of GridSub! I tried to reach RowDataBound event of GridSub but when i call after DataBind(); it doesnt fire, when i call befor it fires with header (not type of dataRow)! i dont know how can i fix it..

Here is my sample code.. I want to reach HyperLink to make its font bold!

Please ignore the names of grid's.. above i just gave an example for the names! PLS HELP

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
<asp:GridView ID="gridSub" runat="server" AutoGenerateColumns="false" ShowHeader="false"
                                    GridLines="None" DataKeyNames="ID" onrowdatabound="gridSub_RowDataBound" >
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <li>
                                                    <asp:HyperLink ID="hlSub" runat="server" CssClass="Hyper" Text='<%# Eval("Name") %>'></asp:HyperLink>
                                                    <asp:Label ID="lblGizli" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
                                                    <asp:Panel ID="pnlDetay" runat="server">
                                                        <ul>
                                                           
                                                                <asp:GridView ID="gridSubSub" runat="server" AutoGenerateColumns="false" ShowHeader="false"
                                                                    GridLines="None">
                                                                    <Columns>
                                                                        <asp:TemplateField>
                                                                            <ItemTemplate>
                                                                                <li>
                                                                                    <asp:HyperLink ID="hlSubSub" CssClass="Hyper" runat="server" Text='<%# Eval("Name") %>'></asp:HyperLink><asp:Label ID="lblSubGizli" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
                                                                                </li>
                                                                            </ItemTemplate>
                                                                        </asp:TemplateField>
                                                                    </Columns>
                                                                </asp:GridView>
                                                          
                                                        </ul>
                                                    </asp:Panel>
                                                </li>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>


 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
protected void gridMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView Grd = (GridView)e.Row.FindControl("gridSub");
                Label lblB = (Label)e.Row.FindControl("lblGizli");
                HyperLink hlMy = (HyperLink)e.Row.FindControl("hlSub");
                if (lblB.Text==hfSayfaID.Value)
                {
                    hlMy.CssClass = "selected";
                    hlMy.Font.Bold = true;
                }


               
                Grd.DataSource = myDB.Pages.Where(a=>a.MotherPageID.ToString() == lblB.Text).ToList();
// ** when there it fire with header not like DataRow
                Grd.DataBind();
// when there it doesnt fire! *******
                Grd.RowDataBound += new GridViewRowEventHandler(Grd_RowDataBound);
                pnlMy.Visible = true;
                if (Grd.Rows.Count == 0)
                {
                    pnlMy.Visible = false;
                }
                else
                {
                    
                   //sth..
                }
              
            }
        }
//this is rowdatabound of gridsub
void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblB = (Label)e.Row.FindControl("lblGizli");
                HyperLink hlMy = (HyperLink)e.Row.FindControl("hlSub");
                if (lblB.Text == hfSayfaID.Value)
                {
                    hlMy.Font.Bold = true;
                }
            }
        }

Answer : Nested Gridviews RowDataBound Event Problem

It simple.
What u can do is On the first grid view create its row databound event.
that u know.
NOw for the gridview inside main grid view You have to manually type ur Rowdatabound event for the sub gridview. and it will fire ur sub grid view onrowdatbound event and you can access those variables inside sub gridview with the same way u access in master grid view.

Some example:

<asp:GridView ID="gdProject" runat="server" AutoGenerateColumns="False"
                onrowdatabound="gdProject_RowDataBound" Width="100%" >
           <Columns>
                <asp:TemplateField>
                    <ItemTemplate>

                        <asp:Label ID="pid" runat="server" Text='<%# Eval("projectId")%>' Visible="false" ></asp:Label>

                        <asp:GridView ID="gdTodoList" runat="server" AutoGenerateColumns="false"  GridLines="None" onrowdatabound="gdTodoList_RowDataBound" ShowHeader="false">

                          <asp:Label ID="tid" runat="server" Text='<%# Eval("taskListID")%>' Visible="false"></asp:Label>

                        </asp:GridView>

                </ItemTemplate>
                </asp:TemplateField>
                </Columns>
</asp:GridView>


IN code behind:

protected void Page_Load(object sender, EventArgs e)
{
gdProject.DataBind();
}

protected void gdProject_RowDataBound(object sender, GridViewRowEventArgs e)
{
        GridView gd = new GridView();
        Label lblPID = new Label();

        gd = (GridView)e.Row.FindControl("gdTodoList");
        lblPID = (Label)e.Row.FindControl("pid");

if (gd != null)
        {
......
 gd.DataBind();
}
}


protected void gdTodoList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
Label lblTID = new Label();
lblTID = (Label)e.Row.FindControl("tid");
}
Random Solutions  
 
programming4us programming4us