5

当gridview中没有用于从页脚插入数据的数据时如何显示页脚。

4

3 回答 3

3

最简单的方法是绑定一个长度为 1 的数组。您可以在其中放入任何您想确定这是虚拟行的内容。在您的 GridViews RowDataBound 方法上检查数据项是否是虚拟行(在尝试检查数据之前,首先确保 RowType 是 DataRow)。如果它是虚拟行,则将行可见性设置为 false。页脚和页眉现在应该显示没有任何数据。

确保在 GridView 上将 ShowFooter 属性设置为 true。

例如。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostback)
    {
         myGrid.DataSource = new object[] {null};
         myGrid.DataBind();
    }
}    

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem == null)
        {
             e.Row.Visible = false;
        }
    }
}
于 2009-04-27T11:35:52.983 回答
2

这是我制作的一些简单的东西:

    /// <summary>
    /// Ensures that the grid view will contain a footer even if no data exists.
    /// </summary>
    /// <typeparam name="T">Where t is equal to the type of data in the gridview.</typeparam>
    /// <param name="gridView">The grid view who's footer must persist.</param>
    public static void EnsureGridViewFooter<T>(GridView gridView) where T: new()
    {
        if (gridView == null)
            throw new ArgumentNullException("gridView");

        if (gridView.DataSource != null && gridView.DataSource is IEnumerable<T> && (gridView.DataSource as IEnumerable<T>).Count() > 0)
            return;

        // If nothing has been assigned to the grid or it generated no rows we are going to add an empty one.
        var emptySource = new List<T>();
        var blankItem = new T();
        emptySource.Add(blankItem);
        gridView.DataSource = emptySource;

        // On databinding make sure the empty row is set to invisible so it hides it from display.
        gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem == (object)blankItem)
                e.Row.Visible = false;
        };
    }

要调用它,您可以使用以下命令:

        MyGridView.DataSource = data;
        EnsureGridViewFooter<MyDataType>(MyGridView);
        MyGridView.DataBind();

希望这可以帮助。干杯!

于 2013-10-17T20:10:29.073 回答
0

这是在 GridView 中有空数据时显示页脚的简单方法。

于 2011-09-12T06:32:00.077 回答