Friday, August 8, 2008

Nested GridView with Paging for Child Grid


I have a post called Nested GridView using JavaScript in this same blog.

A guy at ASP.NET Forums asked how to implement paging for the child grid and this is what made me make a post on this.

Just change the ASPX.CS like this.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            BindGridToDS(GridView1.DataKeys[e.Row.RowIndex].Value.ToString(), (GridView)e.Row.FindControl("GridView2"));
        }
    }


    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gvwChild = ((GridView)sender);
        GridViewRow gvRowParent = ((GridView)sender).Parent.Parent as GridViewRow;

        gvwChild.PageIndex = e.NewPageIndex;
        BindGridToDS(GridView1.DataKeys[gvRowParent.RowIndex].Value.ToString(), gvwChild);

        //show the div again after postback
        string strDIVID = "div" + GridView1.DataKeys[gvRowParent.RowIndex].Value.ToString();
        string cScript = "";
        ClientScript.RegisterStartupScript(typeof(Page), "clientscript", cScript);
    }

    private void BindGridToDS(string strCustomerID, GridView gv)
    {
        SqlDataSource dbSrc = new SqlDataSource();
        dbSrc.ConnectionString = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
        dbSrc.SelectCommand = "SELECT * FROM Orders WHERE CustomerID = '" + strCustomerID + "' ORDER BY OrderDate";
        gv.DataSource = dbSrc;
        gv.DataBind();
    }



Happy Coding!

Related Posts :



1 comments on "Nested GridView with Paging for Child Grid"

Add your comment. Please don't spam!
Subscribe in a Reader
Alexander on October 2, 2009 at 12:01 AM said...

Hi, i have tried the way you have mentioned. I can't figure out the way how to set up the paging thing. Could you give me some info about the thing you do?

Thanks in advance,
Alex

Post a Comment