Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在RowDataBound事件中有条件地绑定gridview行?_C#_Asp.net_Gridview_Rowdatabound - Fatal编程技术网

C# 如何在RowDataBound事件中有条件地绑定gridview行?

C# 如何在RowDataBound事件中有条件地绑定gridview行?,c#,asp.net,gridview,rowdatabound,C#,Asp.net,Gridview,Rowdatabound,我有一个Gridview,用于显示客户付款数据。默认情况下,我使用一些仅在RowDataBound事件中可用的检查来更改包含过期客户的任何行的显示。我想添加过滤数据的选项,以便根据输入仅显示过期或未过期的行。最好的方法是什么 我的想法大致如下: protected void gvTenantList_RowDataBound(object sender, GridViewRowEventArgs e) { if (null != e.Row.DataItem) {

我有一个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
    }
}

基本上,我需要知道
//代码中的内容,以防止显示此行

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

e、 g

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

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

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

我对数据上的这种类型的函数不太熟悉,但乍一看,它确实看起来会起作用,而且比我想象的要干净得多。我只需要有一个条件来控制它使用的数据源分配,基于filter控件。我喜欢它;我会试一试的!为了公平起见,只需使用中间的一个-e.Row.Visible。第一个更好,但是如果你不得不在这上面混上一段时间,那么以后再做。最后一个不太理想,因为它仍然被发送到客户端。我得到了一个运行时异常:[NotSupportedException:Method'Boolean Contains(Int32)'不支持到SQL的转换。]是的,这就是我说的没有那么清楚的意思。您可能需要将datatable或任何您拥有的东西转换为一组业务对象,并对其进行过滤。或者,您可以创建自己的DataView并设置行筛选器属性,但这有点麻烦。我认为执行e.Row.Visible=false选项。
.hidden { display: none; }