Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Grouping data in a DataGridView and referencing the original data

$
0
0

Hi Everybody,

I have written some code that inputs data from 2 x EXCEL spread-sheets, one is a PRICE LIST and the other is an ORDER.

Once I load the ORDER into a DataGridView (DataGridView2) then I use the following code to group the ORDER into PRODUCT SKU's and display in a 3rd DataGridView, which works perfectly:

var result = dataGridView2.Rows.Cast<DataGridViewRow>()
       .Where(r => r.Cells[3].Value != null)
       .Select(r => r.Cells[3].Value)
           .GroupBy(id => id)
              .OrderByDescending(id => id.Count())
              .Select(g => new { Id = g.Key, Count = g.Count()});

The PRODUCT DESCRIPTION is in column 3 - Cells[3], the PRICE is in column 5.

What I am having a problem with is displaying the AVERAGE PRICE of the PRODUCTS. So I would like to average the prices (stored in the 3rd column of DataGridView) of the SELECTED GROUPED PRODUCTS (see below) and display this in a 3rd column below. I have tried adding Price = dataGridView2.Rows[5].Average() after theg.Count(), but this does not work. What am I doing wrong, should I create a SUBSELECTION to get this info?

Any help will be very much appreciated.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>