【问题标题】:Frozen last row of DataGridView as the sum of the columns?将 DataGridView 的最后一行冻结为列的总和?
【发布时间】:2010-03-06 07:05:19
【问题描述】:

是否可以将DataGridView 的最后一行作为列的总和,并且最后一行总是会显示/被冻结?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    解决方案其实很简单,只需要你跳出思维定势。

    通常,一旦数据加载到您的网格视图中,它的底部会出现一个深灰色行:

    您可以充分利用这个空间。您需要做的就是将一些标签拖到表单上,放置在网格视图内,并应用背景颜色:

    在您的代码中,添加如下方法:

    void UpdateTotal(Label Label, int Number)
    {
        // Called from another thread; facilitate safe cross-thread operation
        if(this.InvokeRequired)
            this.Invoke(new Action<Label, int>(UpdateTotal), new object[] {Label, Number});
    
        // Called from this thread or invoked
        else
            // Format the number with a thousands separator
            Label.Text = Number.ToString("N0");
    }
    

    然后,从您更新网格视图的任何位置,调用UpdateTotal(labelPostsAffected, 2000);,使用您的标签名称和您计算的总和(或在方法中计算)。

    结果是这样的:

    【讨论】:

      【解决方案2】:

      是的,这是可能的。 首先,您必须遍历网格并计算所需列的总和 计算总和后,您必须创建一个新的 DataGridRow 并使用计算值填充它,将 DataGridRow 的 Frozen 属性设置为 True,然后将新的 DataGridRow 添加到 Datagridview。 Sum Column Values in DataGridView

      除了创建新的 DataGridRow 之外,您只需在数据源上添加一个包含求和值的新行,然后在 DataGrid 上找到最后一行并将该行的属性设置为 Frozen=True

      【讨论】:

        【解决方案3】:

        为此,您必须设置 True

        ShowFooter 属性

        然后在后面的代码中在页脚中添加您想要的值


           <asp:GridView ID="grdList" runat="server" ShowFooter="True" >
           // other part of gridview codes e.g columns or blah blah blah
           </asp:GridView>
        
           // in code-behind
           int totalValue = 2 * 10;
           grdList.Columns[2].FooterText = totalValue.ToString();
        

        【讨论】:

        • 我找不到这个属性,我在哪里可以找到它?
        • @Gold 正在谈论 Winform DataGridView 而您建议使用 ASP.Net 的代码 GridView
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        相关资源
        最近更新 更多