删除 rowDataBound 事件中的 Gridview 行 asp.net c#

saj*_*aha 1 c# asp.net paging gridview rowdatabound

我有一个 gridview,它检查rowDataBound事件上的一些值。我想根据 rowDataBound 中检查的条件删除一些行。我尝试将所有控件放在面板中并隐藏该面板,即,

尝试1:

protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //some other codes here
        //IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
        if (vis > 0)
        {
            Panel1.Visible = false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题 :

这会扰乱分页,因为行被隐藏,但页计数发生并显示剩余可见行的页码。

尝试2:

protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //some other codes here
        //IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
        if (vis > 0)
        {
            gvr.Parent.Controls.RemoveAt(gvr.RowIndex);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题 :

给出错误:

Specified argument was out of the range of valid values.
Parameter name: index at gvr.Parent.Controls.RemoveAt(gvr.RowIndex);
Run Code Online (Sandbox Code Playgroud)

不想编辑数据源,请帮助我。

小智 5

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (somecondition)
            {
                e.Row.Visible = false;
            }
        }
Run Code Online (Sandbox Code Playgroud)