Search code examples
.netvb.netgridviewinfragistics

How to get sum of ultrawingridcolumn into textbox below


I have a requirement where I want to sum TotalCost in below grid and show the sum result in Total Textbox below.

I am using infragistics ultrawingrid here. How can I do that

enter image description here


Solution

  • You could simply loop over the rows collection and for each row convert the value of the specific column to a decimal adding it to a running total. At the end set the text property of the TextBox to the conversion in string of the running total using the predefined currency format specifier

    decimal total = 0m;
    foreach(UltraGridRow row in grid.Rows)
    {
        total += Convert.ToDecimal(row.Cells["TotalCost"].Value);
    }
    textBox1.Text = total.ToString("C");
    

    Keep in mind that the UltraGrid has a Summaries collection property that you can use to show the total of any column directly under the referenced column in a special "Summary" area.

    SummarySettings ss = null;
    ss = grid.Bands[0].Summaries.Add("mySum", 
             SummaryType.Sum, null, 
             grid.Bands[0].Columns["TotalCost"], 
             SummaryPosition.UseSummaryPositionColumn, 
             grid.Bands[0].Columns["TotalCost"]);
    ss.DisplayFormat = "{0:C}";
    ss.Appearance.ForeColor = Color.Red;
    ss.Appearance.TextHAlign = HAlign.Right;
    grid.DisplayLayout.Override.SummaryDisplayArea = SummaryDisplayAreas.BottomFixed;