我有一个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
答案 0 :(得分:1)
为什么不在进行绑定之前进行过滤?
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
或者您可以使用
将行设置为不可见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;
}
}
}
或者你可以添加一个名为'hidden'的CssClass并在css中添加
.hidden { display: none; }
但是在大多数情况下,我认为你应该只对你真正想要的东西进行数据绑定,并将这样的业务逻辑从绑定事件中删除。