How to average column values of several selected row of a dataGrid?

Consider a dataGrid with 3 columns of data...ID_Code, Product_A, Product_B. And for this example, there are 8 rows
of data.  The items in the Product columns are quantities of products. Let's say the user wishes to select any
combination of 4 rows. Could you provide code examples of how to capture and average the values of Product_A and the
values of Product_B for the selected rows? In other words, What is the average value of Product_A for the 4 selected
rows and what is the average value of Product_B for the 4 selected rows?

I am using VB.Net and Access database. Thank you for your assistance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
first i don't believe the datagrid allows multiselect by default.  you would have to add that functionality along with a way to store the collection of rows selected.  Another option would be to add a datagridboolcolumn to "select" the rows you want.

IT_SteveAuthor Commented:
Could you point me to an example of that coding?
Brian CroweDatabase AdministratorCommented:
which method are you interested in?
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

IT_SteveAuthor Commented:
How about the  datagridboolcolumn to "select" the rows I want. Thank you.
Brian CroweDatabase AdministratorCommented:
you just need to add another column to your datasource, a boolean column, default the value to false.  How are you loading your data? DataReader? DataAdapter? Is the datatable predefined? Add a datagridboolcolumn to your datagridtablestyle and map it to the new column.

below is a link to some code that will help you trap changes to the boolcolumn

To calculate the aggregate values just create a dataview on your datasource with the rowfilter to include only those records with the boolcolumn value set to true.
Datagrid allows multiselect as long as the rowheaders are visible. Although adding a boolean column is an option, you don't have to. When the user holds down the Ctrl key while clicking on the rowheaders, multiple rows are selected. If the rows to be selected are together, the user can hold down the left mouse button at the first row 's header and release the button at the last row's header. Anyway, just to show you how the multiselect can be done at user's end.

Let's say you want to display the average in the Button Click event after the user has selected the rows. Here is the code that does the job:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim qtyA As Integer = 0
        Dim qtyB As Integer = 0
        Dim count As Integer = 0
        For i As Integer = 0 To CType(DataGrid1.DataSource, DataTable).Rows.Count - 1 ' Assume your datasource is a datatable
            If DataGrid1.IsSelected(i) Then
                count += 1
                qtyA += DataGrid1.Item(i, 1) ' 1 is Product A column index
                qtyB += DataGrid1.Item(i, 2) ' 2 is Product B column index
            End If

        Dim avgQtyA As Integer = qtyA \ count  ' \ will give you an integer
        Dim avgQtyB As Integer = qtyB \ count

        Console.WriteLine("Product A Average: " & avgQtyA)
        Console.WriteLine("Product B Average: " & avgQtyB)

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT_SteveAuthor Commented:
Both solutions solve my problem. The solution from Amyhxu solved it the way I was originally thinking about the problem and BriCrowe's solutions helped solve another way I might want to deal with selected rows later. Thank you both for your assistance and sharing your experience with me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.