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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/808468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:02:47  来源:igfitidea点击:

Can I Merge Footer in GridView?

c#.netasp.nethtml

提问by MrM

I have a GridView that is bound with a dataset. I have my footer, whichis separated by the column lines. I want to merge 2 columns; how do I do that?

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

采纳答案by mangokun

untested code

未经测试的代码

1st footer template should include <%# GetTotal() %>

第一个页脚模板应包含 <%# GetTotal() %>

2nd footer template should be empty

第二页脚模板应为空

    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

回答by aape

I was doing something like this - trying to have a button, in the footer span multiple cols.

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

I ran into a problem when I set columnspan via code, because a) I'm a noob, and b) it was notdoing what I expected. I don't remember all the details, but there was some kind of gotcha in there - like it was adding extra columns or something.

我在通过代码设置 columnspan 时遇到了问题,因为 a) 我是个菜鸟,b) 它没有按照我的预期做。我不记得所有的细节,但那里有一些问题 - 就像添加额外的列或其他东西一样。

Here was my solution. Maybe some of it will be useful. I did in the prerender for the gridview (gvDocs).

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

And what got it working correctly for me, was programatically removing cells of the footer as well as setting the columnspan.

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

Even if the code doesn't help, maybe people will get a laugh at the encroaching forgetfulness afflicting me. It makes me laugh sometimes.

即使代码没有帮助,也许人们会嘲笑折磨我的健忘。它有时让我发笑。

   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 - Here it is in C#

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
//=======================================================

EDITED - Needed to use square brackets to access cell by index in the footer row

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

回答by Atif Riaz

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;

        }

    }