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

Posted on 2006-03-27
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.
Question by:IT_Steve
    LVL 34

    Expert Comment

    by:Brian Crowe
    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.


    Author Comment

    Could you point me to an example of that coding?
    LVL 34

    Expert Comment

    by:Brian Crowe
    which method are you interested in?

    Author Comment

    How about the  datagridboolcolumn to "select" the rows I want. Thank you.
    LVL 34

    Assisted Solution

    by:Brian Crowe
    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.
    LVL 14

    Accepted Solution

    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

    Author Comment

    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.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now