문제

I have a summary column coded like this:

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;
        }
    }

I need to format this column in this way like for the others which I have modified in the designer.

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

enter image description here

How can I achieve such format of the string in the above context?

Update I tried something on this line but the compiler gives error saying that the string format can be only "G", "g", "X", "x", "F", "f", "D" o "d" and if I try one of those than I get an error "Target invocation error not managed".

new GridViewSummaryItem("Balance", "{0}", GridAggregateFunction.Sum.ToString("R #.##,##"))}));
도움이 되었습니까?

해결책

In the constructor you can specify the desired format of the summary item, so try this:

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

Two other things I noticed:

  1. You are adding the row to SummaryRowsBottom and the you are setting its PinPosition to Top. Instead, you can use the SummaryRowsTops.
  2. I noticed you are creating a new instance of a Font in the formatting event. Bear in mind that this event gets triggered quite often and creating a Font is not a cheap operation, so I would move this as a global variable.

다른 팁

This may solve your problems:

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

There is a method called string.Format() where you can modify or create a new string exactly how you need it.

Hope it helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top