【问题标题】:Make GridView footer visible when there is no data bound没有数据绑定时使 GridView 页脚可见
【发布时间】:2010-10-22 00:55:37
【问题描述】:

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

【问题讨论】:

    标签: asp.net .net gridview visibility footer


    【解决方案1】:

    最简单的方法是绑定一个长度为 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;
            }
        }
    }
    

    【讨论】:

    • 当自动生成列 == false 时这不起作用,还有其他想法吗?
    • 应该和我之前做过很多次一样。你确定你在页面加载时绑定了一些东西吗?
    • 这对我不起作用,因为我在网格定义中指定了 DataKeyNames。移除了DataKeyNames和grid在没有数据时只显示footer时正常显示。
    • 另外,当您使用 asp:BoundField 控件时,您不能使用此解决方案。您必须对所有内容进行模板化。
    【解决方案2】:

    Here is the simple way GridView 有空数据时显示页脚。

    【讨论】:

      【解决方案3】:

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

          /// <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-02-13
        • 2011-07-19
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 2013-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多