C# .NET - How to call one GridView RowDataBound Event in another GridView RowDataBound ?

Asked By Ganapati Bhat on 10-Dec-09 02:05 AM

How to call one GridView RowDataBound Event in another GridView RowDataBound ?

please help me

thanks in advance..

Events are not bound to be called

Huggy Bear replied to Ganapati Bhat on 10-Dec-09 02:21 AM
I think you have misunderstood the concepts of events, they are not bound to be called at required places by designed. Instead they will get fired only when the appropriate event occurs. 

If you want to make the event GridView1_RowDataBound to be fired from GridView2_RowDataBound, then bind the data to GridView1 in GridView2_RowDataBound event.

But your requirement doesn't seem to be a valid one though. If it is possible for you to provide a low level view of your requirement I can suggest a better approach.

RE

Ganapati Bhat replied to Huggy Bear on 10-Dec-09 03:54 AM
I have one grid , within that i have taken another grid. So i have taken RowDataBound event for both Grid. But it is firing only RowDatabind event of 1st grid. But i want to fire 2nd grid event. How to achieve this?

Re

Huggy Bear replied to Ganapati Bhat on 10-Dec-09 04:03 AM
You have to bind the data for the second grid in the RowDataBound event of the first grid like

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType.Equals(DataControlRowType.DataRow))
    {
        //Since you said you have a grid view inside a gridview.
        GridView gridView2 = e.Row.FindControl("GridView2") as GridView;
        //Assuming GetData() will return the datasource
        gridView2.DataSource = GetData();
        //When DataBind is called, the RowDataBound event for
        //gridview2 will be fired
        gridView2.DataBind();
    }
}
RE
Ganapati Bhat replied to Huggy Bear on 10-Dec-09 04:23 AM

No. This is not working. Here situation is..

I have one grid named as grid1. Inside this grid i have anothe grid named as grid2. Inside this grid2 i have two labels which fetches some values from Database. I need to show this label values when the page is loaded. So i am planning to use RowDatabound event where i will get those laabel values. right? So those label values will be available only in grid2 Rowdatabound. It is not accessible in grid1 Rowdatabound. here i can fire only grid1 rowdatabound event when page is loaded. But how to fire grid2 Rowdatabound event?

this is my actual problem. 

Re
Huggy Bear replied to Ganapati Bhat on 10-Dec-09 04:48 AM
If you have the label values filled in grid2 already, why you want the RowDataBound event of grid2 to be fired?
RE
Ganapati Bhat replied to Huggy Bear on 10-Dec-09 05:00 AM
No. I have taken labels in grid2. But how to access those labels in Program(C#)? If i take those labels in Rowdatabound event of grid1 its throwing error. Because these labels can't access in Grid1rowdatabound.
Re
Huggy Bear replied to Ganapati Bhat on 10-Dec-09 06:05 AM
You don't have to access Label of grid2 on the server side. What you can do is set the Label values in Grid2 on the .aspx page itself as shown below.

Template fields of grid2

<asp:TemplateField HeaderText="Label1">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%DataBinder.Eval(Container.DataItem, "Columen1") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label2">
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%DataBinder.Eval(Container.DataItem, "Columen2") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
RE
Ganapati Bhat replied to Huggy Bear on 11-Dec-09 01:29 AM

Hi Huggy Bear,

Thanks a lot for your reply. I have used some other logic which is working fine to this problem.