Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top