0

I have a Gridview, for displaying customer payment data. By default, I alter the display of any rows containing past due customers, using some checks that are only easily available in the RowDataBound event. I would like to add the option to filter out the data to only show rows which are or are not past due, based on an input. What is the best way to do this?

I'm thinking something along the lines of:

protected void gvTenantList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (null != e.Row.DataItem)
    {
        DataRowView rowView = (DataRowView)e.Row.DataItem;
        if (hsPastDueLeases.Contains((int)rowView["LeaseID"]))
        {
            e.Row.CssClass += " pinkbg";
            if (showCurrentOnly) //code to prevent showing this row
        }
        else if (showPastDueOnly) //code to prevent showing this row
    }
}

Basically, I need to know what belongs in the //code to prevent showing this row

Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65

1 Answers1

1

Why don't you do the filtering before you do the bind?

e.g.

gvTenantList.DataSource = data.Where(a=> !hsPastDueLeases.Contains(a.LeaseID)); // Of course you have a datatable so this is not 100% as easy as this

Or you can set the row to be invisible using

e.Row.Visible = false;


protected void gvTenantList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (null != e.Row.DataItem)
    {
        DataRowView rowView = (DataRowView)e.Row.DataItem;
        if (hsPastDueLeases.Contains((int)rowView["LeaseID"]))
        {
            e.Row.CssClass += " pinkbg";
            e.Row.Visible = !showCurrentOnly;
        }
        else if (showPastDueOnly){ //code to prevent showing this row
            e.Row.Visible = false;
        }
    }
}

Alternatively you could add a CssClass called 'hidden' and in css have

.hidden { display: none; }

But for the most part I think you should only databind what you really want and leave the business logic like this out of the binding events.

Dave Walker
  • 3,498
  • 1
  • 24
  • 25
  • I'm not very familiar with this type of function-on-the-data, but at first glance, it does look like it will work, and much more clean than what I was thinking. I'd just have a condition to control which Datasource assignment it uses, based on the filter control. I like it; will give it a shot! – Jeffrey Blake Dec 01 '11 at 23:28
  • To be fair to just get it going use the middle one - e.Row.Visible. The first is better but if you have to muck around with it for quite a while then do that later. The last is less desirable because it is being sent to teh client still. – Dave Walker Dec 01 '11 at 23:30
  • I'm getting a run-time exception on this: [NotSupportedException: Method 'Boolean Contains(Int32)' has no supported translation to SQL.] – Jeffrey Blake Dec 01 '11 at 23:32
  • Yeah thats what I mean when I said it's not so clear as doing that. You'll probably have to convert the datatable or whatever you have into a set of Business Objects and do the filtering on that. Alternatively you can create your own DataView and set the row filter property - but that is a bit nasty. Do the e.Row.Visible = false option I think. – Dave Walker Dec 01 '11 at 23:34