【问题标题】:Format string of summary column in radGridViewradGridView中汇总列的格式字符串
【发布时间】:2014-01-06 05:27:09
【问题描述】:

我有一个汇总列,编码如下:

private void radGridView1_Initialized(object sender, EventArgs e)
    {          
        this.radGridView1.SummaryRowsBottom.Add(new GridViewSummaryRowItem(new GridViewSummaryItem[]{
        new GridViewSummaryItem("Balance", "{0}", GridAggregateFunction.Sum)}));
        this.radGridView1.MasterTemplate.ShowTotals = true;
        this.radGridView1.MasterView.SummaryRows[0].PinPosition = PinnedRowPosition.Top;
    }

private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement is GridSummaryCellElement)
        {
            e.CellElement.TextAlignment = ContentAlignment.MiddleRight;
            Font summaryFont = new Font("Segoe", 9, FontStyle.Bold);
            e.CellElement.Font = summaryFont;
        }
    }

我需要像我在设计器中修改的其他列一样格式化此列。

{0:R #.##,##}

如何在上述上下文中实现这种格式的字符串?

更新 我在这一行尝试了一些东西,但编译器给出错误,说字符串格式只能是“G”、“g”、“X”、“x”、“F”、“f”、“D”o“d”如果我尝试其中之一,则会收到错误“未管理目标调用错误”。

new GridViewSummaryItem("Balance", "{0}", GridAggregateFunction.Sum.ToString("R #.##,##"))}));

【问题讨论】:

    标签: c# winforms telerik radgridview


    【解决方案1】:

    在构造函数中你可以指定摘要项的格式,试试这个:

      this.radGridView1.SummaryRowsBottom.Add(new GridViewSummaryRowItem(new GridViewSummaryItem[]{
                    new GridViewSummaryItem("DecimalColumn", "{0:R #.##,##}", GridAggregateFunction.Sum)}));
    

    我注意到的另外两件事:

    1. 您正在将行添加到 SummaryRowsBottom 并且您正在将其 PinPosition 设置为 Top。相反,您可以使用 SummaryRowsTops。
    2. 我注意到您正在格式化事件中创建一个新的字体实例。请记住,这个事件经常被触发,并且创建一个字体并不是一个便宜的操作,所以我会将它作为一个全局变量来移动。

    【讨论】:

    • 非常感谢您为我的问题提供了解决方案,并为其他上下文增加了价值。非常感谢您的回答和支持。
    【解决方案2】:

    这可能会解决您的问题:

    http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx

    有一个名为 string.Format() 的方法,您可以在其中完全按照您的需要修改或创建一个新字符串。

    希望对你有帮助!

    【讨论】:

    • Vinicius,我知道如何格式化字符串。那不是真正的问题。问题是如何在上述上下文中格式化字符串。
    猜你喜欢
    • 2017-03-22
    • 2021-04-08
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多