1

I have a gridview which checks some values on rowDataBound event.I want to remove some rows based on conditions checked in rowDataBound.I tried putting all controls in a Panel and hiding that panel ie,

TRY 1 :

protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //some other codes here
        //IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
        if (vis > 0)
        {
            Panel1.Visible = false;
        }
    }
}

PROBLEM :

This messes up the paging since rows are hidden but page count occurs and shows page numbers for the remaining visible rows.

TRY 2 :

protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //some other codes here
        //IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
        if (vis > 0)
        {
            gvr.Parent.Controls.RemoveAt(gvr.RowIndex);
        }
    }
}

PROBLEM :

gives error :

Specified argument was out of the range of valid values.
Parameter name: index at gvr.Parent.Controls.RemoveAt(gvr.RowIndex);

dont want to edit datasource, help me out guys .

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • You may have to edit your datasource as page information are calculated from the datasource – codingbiz Dec 21 '12 at 05:52
  • 1
    Please check out the below link http://stackoverflow.com/questions/592106/how-to-delete-row-from-gridview – Sumant Dec 21 '12 at 06:38

1 Answers1

5
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (somecondition)
            {
                e.Row.Visible = false;
            }
        }
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
LNRao
  • 169
  • 4
  • row visibility wont affect paging and will mess it. – sajanyamaha Dec 21 '12 at 16:44
  • Only problem with this approach is that you are gonna have variable page size when you take this approach depending on how many rows get hidden on each page, lol. – drzounds Oct 20 '15 at 19:51