Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-27
7
Medium Priority
?
290 Views
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.
0
Comment
Question by:IT_Steve
  • 3
  • 3
7 Comments
 
LVL 34

Expert Comment

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

0
 

Author Comment

by:IT_Steve
ID: 16313199
Could you point me to an example of that coding?
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16313564
which method are you interested in?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:IT_Steve
ID: 16314355
How about the  datagridboolcolumn to "select" the rows I want. Thank you.
0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 400 total points
ID: 16315445
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

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q874q

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.
0
 
LVL 14

Accepted Solution

by:
amyhxu earned 1600 total points
ID: 16352554
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
        Next

        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
0
 

Author Comment

by:IT_Steve
ID: 16355406
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

564 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