views:

244

answers:

1

Hello,

I have nested a repeater control in Gridview. Right now it is showing gridview rows and repeater header for every case(whether data is there or not for that particular grid view row in the repeater control). I want to hide the gridview row and repeater control header when there is no data present for that particular gridview row.

Thanks, That case I handled at code level by filtering the resulted data table.

Now the another problem I am facing: I have allowed the paging on the gridview i.e. pagesize 3. When page loads it works fine, but when I go to page 2 then it generates following error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Below is the code to fill the grid, paging and fill repeater on rowdatabound event of grid.

private void FillGrid() { clsCustomFunctions objShort = new clsCustomFunctions(); grd1.DataSource = objShort.GetAll();
}

protected void grd1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    try
    {
        FillGrid();
        grd1.PageIndex = e.NewPageIndex;
        grd1.DataBind();
    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.Message;
    }
}

protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    clsCustomFunctions objShort = new clsCustomFunctions();        
    if (e.Row.RowType == DataControlRowType.DataRow)
    {            
        ((HtmlTable)e.Row.FindControl("gridTable")).BgColor = "#006699";
        Repeater rpt = (Repeater)e.Row.FindControl("rpt1");
        rpt.DataSource = objShort.GetResult(Convert.ToInt32(grd1.DataKeys[e.Row.DataItemIndex].Value));
        rpt.DataBind();
    }
}

grd1.DataKeys[e.Row.DataItemIndex].Value line is throwing error. How to handle this to pass values of page 2 only.

A: 

Try handling the OnRowDataBound event of the grid. This gives you a GridViewRowEventArgs object (say e).

You can then look at e.Row.DataItem to get the data it is binding to to check if you need to hide the header.

You can use e.Row.FindControl("RepeaterName") to get the repeater to manipulate as you want.

Kim R