Showing posts with label gridview with javascript. Show all posts
Showing posts with label gridview with javascript. Show all posts

Friday, August 8, 2008

Nested GridView with Paging for Child Grid

1 comments
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!
Read more...