【问题标题】:Can I Merge Footer in GridView?我可以在 GridView 中合并页脚吗?
【发布时间】:2010-10-22 22:46:35
【问题描述】:

我有一个与数据集绑定的 GridView。我有我的页脚,它由列线分隔。我想合并 2 列;我该怎么做?

<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
...  
</ItemTemplate>
<FooterTemplate >                    
Grand Total:
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<ItemTemplate>
...  
</ItemTemplate>
<FooterTemplate >                    
<%# GetTotal() %> 
</div>
</FooterTemplate>
</asp:TemplateField>

【问题讨论】:

标签: c# .net asp.net html


【解决方案1】:
protected void GridView1_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Footer)
        {
           e.Row.Cells.RemoveAt(1);
           e.Row.Cells[0].ColumnSpan = 2;

        }

    }

【讨论】:

    【解决方案2】:

    未经测试的代码

    第一个页脚模板应包含

    第二个页脚模板应该是空的

        Protected Sub Page_SaveStateComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SaveStateComplete
            Dim DG As GridView = GridView1
                Dim Tbl As Table = DG.Controls(0)
                Dim tr As GridViewRow
                Dim i As Integer
                Dim j As Integer
    
    tr = Tbl.Rows(Tbl.Rows.Count - 1) 'this line assume last row is footer row
    
                        tr.Cells(0).ColumnSpan = 2 'if you have 3 columns then colspan = 3 instead
    
                        For j = 1 To 1 'if you have 3 columns then j = 1 To 2 instead
                            tr.Cells(j).Visible = False
                        Next
    
        End Sub
    

    【讨论】:

      【解决方案3】:

      我正在做这样的事情 - 试图在页脚中有一个按钮跨越多个列。

      我在通过代码设置 columnspan 时遇到了一个问题,因为 a) 我是菜鸟,b) 它没有 符合我的预期。我不记得所有的细节,但其中有一些陷阱——比如添加额外的列之类的。

      这是我的解决方案。也许其中一些会很有用。我在 gridview (gvDocs) 的预渲染中做了。

      让我正常工作的原因是以编程方式删除页脚的单元格以及设置列跨度。

      即使代码没有帮助,也许人们会因为我的健忘问题而发笑。它有时让我发笑。

         Protected Sub gvDocs_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvDocs.PreRender
      
              If gvDocs.Rows.Count > 0 Then
      
      
                  Dim m As Integer = gvDocs.FooterRow.Cells.Count
                  For i As Integer = m - 1 To 1 Step -1
                      If i <> 8 Then '7 is the number of the column with the applychanges button in it.
                          gvDocs.FooterRow.Cells.RemoveAt(i)
                      End If
                  Next i
                  gvDocs.FooterRow.Cells(1).ColumnSpan = 6 '6 is the number of visible columns to span.
              End If
          End Sub
      

      Fernando68 - 这是在 C# 中

      protected void gvDocs_PreRender(object sender, System.EventArgs e)
      {
      
          if (gvDocs.Rows.Count > 0) {
      
              int m = gvDocs.FooterRow.Cells.Count;
              for (int i = m - 1; i >= 1; i += -1) {
                  //7 is the number of the column with the applychanges button in it.
                  if (i != 8) {
                      gvDocs.FooterRow.Cells.RemoveAt(i);
                  }
              }
              gvDocs.FooterRow.Cells[1].ColumnSpan = 6;
              //6 is the number of visible columns to span.
          }
      }
      
      //=======================================================
      //Service provided by Telerik (www.telerik.com)
      //Conversion powered by NRefactory.
      //Twitter: @telerik
      //Facebook: facebook.com/telerik
      //=======================================================
      

      已编辑 - 需要使用方括号按页脚行中的索引访问单元格

      【讨论】:

      • @Fernando68 - 添加了 C#
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多