Question

I am doing a project in asp.net 3.5 (c#) where I have used a GridView inside another GridView. The problem, however, is that I dont know how to use the PageIndexChanging Event for the Child GridView.. Anyone with a solution please help me..!! Thanks in advance.. I am uploading the code that fills the two grids..

private void dynamic_GV1()
{
    DataSet ds_news_details = new DataSet();
    DataSet ds_pic_preview = new DataSet();
    ds_news_details = BL_News.News_Details_Top10_Select();
    if (ds_news_details.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds_news_details;
        GridView1.DataBind();
        int counter;
        for(counter = 0 ; counter < ds_news_details.Tables[0].Rows.Count; counter++)
        {
            GridView gv_pic = (GridView)GridView1.Rows[counter].FindControl("GridView2");
            ds_pic_preview = BL_News.News_Pictures_Select(Convert.ToInt32(ds_news_details.Tables[0].Rows[counter][0].ToString()));
            gv_pic.DataSource = ds_pic_preview;
            gv_pic.DataBind();
        }
    }
}
Was it helpful?

Solution

You can attach the event handler to your sub-GridViews programmatically in that same loop (where you are binding data to them). Note that this assumes you want to handle them all with the same function:

for(counter = 0 ; counter < ds_news_details.Tables[0].Rows.Count; counter++)
{
    GridView gv_pic = (GridView)GridView1.Rows[counter].FindControl("GridView2");

    // Attach event handler here
    gv_pic.PageIndexChanging += yourEventHandlerName;

    ds_pic_preview = BL_News.News_Pictures_Select(Convert.ToInt32(ds_news_details.Tables[0].Rows[counter][0].ToString()));
    gv_pic.DataSource = ds_pic_preview;
    gv_pic.DataBind();
}

Where "yourEventHandlerName" is the name of the function that you want to use as the PageIndexChanging event handler:

protected void yourEventHandlerName(Object sender, GridViewPageEventArgs e)
{

}

You can cast the "sender" variable back to a GridView to examine it and figure out which sub-GridView fired the event, I imagine. Something like this should work:

GridView currentGrid = (GridView)sender;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top