我可以在GridView中合并页脚吗?

时间:2009-04-30 18:45:37

标签: c# .net asp.net html

我有一个与数据集绑定的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>

3 个答案:

答案 0 :(得分:6)

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;

        }

    }

答案 1 :(得分:2)

未经测试的代码

第一个页脚模板应包含&lt;%#GetTotal()%&gt;

第二个页脚模板应为空

    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

答案 2 :(得分:1)

我正在做这样的事情 - 尝试在页脚中插入一个按钮,跨越多个cols。

当我通过代码设置columnspan时遇到了问题,因为a)我是菜鸟,而b)按照我的预期进行操作。我不记得所有的细节,但那里有一些问题 - 比如它增加了额外的列或其他东西。

这是我的解决方案。也许其中一些会有用。我在网格视图(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
//=======================================================

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