We help IT Professionals succeed at work.

Binding inner gridview

Sukeshjph asked
I am binding the inner gridview in the onrowdatabound of outer grid but i am not able do paging for the inner gridview which loads in the outer gridview rowdatabound method.
Watch Question

On DataRowBound event of outer gridview set the datasource of child gridview. After setting, keep this datasource in ViewState . like;


And then on pageindexchanging event of inner gridview do this.

        GridView grdUserChild = (GridView)sender;

        DataTable dt = (DataTable)ViewState["InnerGridDS"];
        grdUserChild.PageIndex = e.NewPageIndex;
        grdUserChild.DataSource = dt;

It works for me. So should work for you too :)


In my outer gridview rowcommand i am writing code like this.
DateTime Span = System.DateTime.Today.AddDays(-8);
        DateTime StartTime;
        DateTime EndTime;
        StartTime = Convert.ToDateTime(Span.AddDays(1).ToString());
        EndTime = Convert.ToDateTime(Span.AddDays(8).ToString());
        serviceObject = new Diaspora.Services.NetworkUpdateService();
        if (e.Row.RowType == DataControlRowType.DataRow)
            DataTable dt = new DataTable();
            LinkButton lnkbtnfriendsname = (LinkButton)e.Row.FindControl("lnkbtnFriendsBuddyID");
            GridView gvInnerForfriendsRowB = e.Row.FindControl("gvInnerForfriends") as GridView;

            if (Convert.ToBoolean(ViewState["DecideForWhich"]) == true)
                dt = serviceObject.FindOutFriendsFriend(Convert.ToInt64(lnkbtnfriendsname.Text), StartTime, EndTime);
                ViewState["InnerGridDS"] = dt;

                dt = serviceObject.FindOutFriendsFriend(Convert.ToInt64(lnkbtnfriendsname.Text), System.DateTime.Today);
                ViewState["InnerGridDS"] = dt;

            gvInnerForfriendsRowB.DataSource = dt;

as u told i also placed viewstate.But when i click on the page buttons then rowcommand event of childgrid is fired as i need to travel into anothe page on the button click.as the row command is fired first i am not getting paging event fired.Can u suggest how can i go to onpageindex changing directly avoding onrowcommand of childgrid

here is rowcommand of child grid.

protected void gvInnerForfriends_rowcommand(object sender, GridViewCommandEventArgs e)
        int index = Convert.ToInt32(e.CommandArgument);
        Response.Redirect("../Default.aspx?Id=" + index);

On row command event you should handle action according to the command name.

create a switch/case on e.CommandName variable.

switch e.CommandName
 case "Select":

case "Page":


When you click a paging button, first row command fires, then page index changing event. On row command event, if you check the e.CommandName, you will see it is Page command. You wont handle it on row command. as I said, after row command paging event will fired. Do your staff there.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.