Search code examples
c#visual-studiodatagridviewwindows-forms-designer

set total of invoices for each customer in DataGridview column based on other calumn valueC# Windows form


Im trying to build a simple dashboard C# windows form, how to put the total amount of number of invoices based on their mobile number as ID:

the DGV as below :

customer name | Mobile Number | Total Due Amount

John Smith | +123 | = total invoices of the cell in column number 1 which is +123

thank you

i used to to calculate the full column for specific customer to labe.text


Solution

  • Since data is stored in the list collection, you need to use linq syntax to group according to id. After grouping the id, the same id can be summed.The reference steps are as follows.

    1. Entity class encapsulates data.

      public class Product
      {
        /// <summary>
        /// product id
        /// </summary>
        public int ID { set; get; }
        /// <summary>
        /// product name
        /// </summary>
        public string Name { set; get; }
        /// <summary>
        /// Product unit price
        /// </summary>
        public double Price { set; get; }
        /// <summary>
        /// Production Date
        /// </summary>
        public DateTime ProductDate { set; get; }
      }
      
    2. Generate test data.

      private void button1_Click(object sender, EventArgs e)
       {
           //Generate test data
            list = new List<Product>();
           Random r = new Random();
           for (int i = 0; i < 5; i++)
           {
               float iran = r. Next(1, 111);
               Product pro = new Product()
               {
                   ID = i + 1,
                   Name = "BMW" + i.ToString(),
                   Price = iran * 1000,
                   ProductDate = DateTime. Now. AddDays(i)
               };
               Product pro2 = new Product()
               {
                   ID = i + 1,
                   Name = "BMW" + i.ToString(),
                   Price = iran * 1000,
                   ProductDate = DateTime. Now. AddDays(i)
               };
               list.Add(pro);
               list.Add(pro2);
      
           }
      
      
       }
      
    3. Sum data grouped by id. Data display datagridview control.

      private void button2_Click(object sender, EventArgs e)
       {
           //Sum, find the total price of all products
           var sumResult = from s in list
                               //Group by id and store the grouped result set in p
                           group s by new { s.ID, s.Name} into p
                           //At this time, the result set is already p, so we need to fetch data from p.
                           select new
                           {
      
                               name = p.Key.Name,
                               key = p.Key,
                               sum = p.Sum(x => x.Price),
                               min = p.Min(x => x.Price),
                               max = p.Max(x => x.Price),
                               average = p.Average(x => x.Price),
                               count = p. Count()
                           };
      
           foreach (var item in sumResult)
           {
      
               dataGridView1.Rows.Add(item.name, item.key.ID, item.sum);
      
           }
      
    4. Test results.

      enter image description here