0

我有一个 Gridview,用于显示客户付款数据。默认情况下,我使用一些仅在 RowDataBound 事件中很容易获得的检查来更改包含过期客户的任何行的显示。我想添加选项以根据输入过滤掉数据以仅显示过期或未过期的行。做这个的最好方式是什么?

我在想一些事情:

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
    }
}

基本上,我需要知道什么属于//code to prevent showing this row

4

1 回答 1

1

为什么在绑定之前不进行过滤?

例如

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

或者您可以使用将行设置为不可见

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;
        }
    }
}

或者,您可以添加一个名为“隐藏”的 CssClass,并且在 css 中有

.hidden { display: none; }

但在大多数情况下,我认为您应该只对您真正想要的内容进行数据绑定,并将这样的业务逻辑排除在绑定事件之外。

于 2011-12-01T23:22:45.287 回答