Lets say we have a datagridview that is looking similar to this -

Smart_Codes 1 Reputation point
2021-07-11T08:39:56.643+00:00

Lets say we have a datagridview that is looking similar to this in c#:
113556-1.png
I want that when I click the button, my program should look for uinique TaxRate and sum up the records so that I could get the following result using c#

113510-2.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,198 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-07-11T10:57:22.217+00:00
    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,031 Reputation points
    2021-07-11T11:48:58.96+00:00

    Here is a complete example which does grouping as asked. I didn't setup formatting of columns in the DataGridView.

    113589-grouped.png

    Data class using mocked data

    using System.Data;  
    using System.Linq;  
      
    namespace GroupbyDataTable  
    {  
        public class Operations   
        {  
            public static DataTable Mocked()  
            {  
                var table = new DataTable();  
      
                table.Columns.Add(new DataColumn("TaxRate", typeof(decimal)));  
                table.Columns.Add(new DataColumn("Value", typeof(decimal)));  
                table.Columns.Add(new DataColumn("TaxAmount", typeof(decimal)));  
                table.Columns.Add(new DataColumn("FinalValue", typeof(decimal),   
                    "Value + TaxAmount"));  
      
                table.Rows.Add(5, 1000, 50);  
                table.Rows.Add(5, 2000, 100);  
                  
                table.Rows.Add(10, 1000, 100);  
                table.Rows.Add(10, 2000, 200);  
                  
                table.Rows.Add(15, 1000, 150);  
                table.Rows.Add(15, 2000, 300);  
      
                return table;  
                  
            }  
      
            public static DataTable GroupData(DataTable dt)  
            {  
      
                return dt.AsEnumerable()  
                    .GroupBy(row => DataRowExtensions.Field<decimal>(row, "TaxRate"))  
                    .Select(grouped =>  
                    {  
                        var row = dt.NewRow();  
      
                        row["TaxRate"] = grouped.Key;  
                        row["Value"] = grouped.Sum(dRow => dRow.Field<decimal>("Value"));  
                        row["TaxAmount"] = grouped.Sum(dRow => dRow.Field<decimal>("TaxAmount"));  
                        row["FinalValue"] = grouped.Sum(dRow => dRow.Field<decimal>("FinalValue"));  
      
                        return row;  
                    }).CopyToDataTable();  
            }  
        }  
    }  
    

    Form code

    using System;  
    using System.Data;  
    using System.Windows.Forms;  
      
    namespace GroupbyDataTable  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
                Shown += OnShown;  
            }  
      
            private void OnShown(object sender, EventArgs e)  
            {  
                dataGridView1.DataSource = Operations.Mocked();  
            }  
      
            private void GroupButton_Click(object sender, EventArgs e)  
            {  
                dataGridView1.DataSource = Operations  
                    .GroupData((DataTable)dataGridView1.DataSource);  
            }  
      
            private void ReloadButton_Click(object sender, EventArgs e)  
            {  
                dataGridView1.DataSource = Operations.Mocked();  
            }  
        }  
    }  
      
    
    1 person found this answer helpful.
    0 comments No comments