We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


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

IT_Steve asked
Medium Priority
Last Modified: 2012-06-27
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.
Watch Question

Brian CroweDatabase Engineer
Top Expert 2005

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.


Could you point me to an example of that coding?
Brian CroweDatabase Engineer
Top Expert 2005

which method are you interested in?


How about the  datagridboolcolumn to "select" the rows I want. Thank you.
Brian CroweDatabase Engineer
Top Expert 2005
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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


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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.