Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculate datagridview base on selected row

Posted on 2010-09-04
24
Medium Priority
?
392 Views
Last Modified: 2012-05-10
Hi All,

I want to show to user summary data from datagridview they select.

I mean while they higlighted the gridview.

How could I do it, what event should I catch to get it ?

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 5
  • 4
24 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33602500
you are talking about summary  columns? or rows?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33602534
To have a sum textboxcolumn you can use...(if that is what you want)

Dim contcolumn As New DataGridViewTextBoxColumn

                With contcolumn
                    .HeaderText = strTest
                    .Width = 200
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .DefaultCellStyle.Font = New Font("Arial", 10, FontStyle.Regular)
                End With
                Me.DataGridView1.Columns.Insert(Me.DataGridView1.ColumnCount, contcolumn)


                Dim y As Integer = 0
                For y = 0 To Me.DataGridView1.Rows.Count - 1
                    Dim iTotal As Decimal = 0
                    With DataGridView1
                        For i As Integer = 2 To .ColumnCount - 1   or you can use as start any column index..
                            If Not IsDBNull(.Rows(y).Cells(i).Value) Then
                                iTotal += .Rows(y).Cells(i).Value
                            End If
                        Next
                        .Rows(y).Cells("SumColumnName").Value = iTotal
                    End With
                Next

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33602677
using the code above you can use then the binding source position change event
Private Sub MyBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBindingSource.PositionChanged
        For Each row As DataGridViewRow In Me.MyDataGridView.Rows
            If Not row.IsNewRow Then
                If Not IsDBNull(row.Cells("SumColumn").Value or row.Cells("SumColumn").Value is Not Nothing then 
                    Me.TextBox1.Text = row.Cells("sumColun").Value
                End If
            End If
        Next
End Sub

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33602749
at the above :
strTest="SumColumn"
and also add in With concolumn
.Name=strTest
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33605499
There are 4 events. You can use any one of them. In the order they are fired:
CellClick
CellMouseClick
CellContentClick
CellMouseUp

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 33608717
Hi All,

I still don't get it. Please see below description of Windows Form.

Header:

Total  99,999
Detail  :

Date   Qty
01       10
02       20
03       10
04       10
05       20
06       30

The user click Date 01, The total show 10. The left mouse still hold.
Then the use drag to Date 03. While drag pass 02, the total show 30, and so on.

How to achieve this ?

Thank you.





0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608724
Try this
 Private Sub MYDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Personnel_Hours_AC_TypesDataGridView.CellClick
        Me.TextBox1.Text = Me.MYDataGridView.Rows(e.RowIndex).Cells("QTY").FormattedValue
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608727
Sorry ...And you can use also double click,Mouse Click
Private Sub MYDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MYDataGridView.CellClick
        Me.TextBox1.Text = Me.MYDataGridView.Rows(e.RowIndex).Cells("QTY").FormattedValue
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608746
Ok I didn't get the question ...
Private Sub MeDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MeDataGridView.CellClick
        If Me.MeDataGridView.Rows.Count > 1 Then
            Dim i As Integer
            For i = 1 To Me.MeDataGridView.Rows.Count - 1
                Me.TextBox1.Text = Me.MeDataGridView.Rows(e.RowIndex).Cells("QTY").FormattedValue
                Me.Text.Take = CInt(Me.TextBox1.Text) + Citn(Me.MeDataGridView.Rows(e.RowIndex - i).Cells("QTY").FormattedValue)
            Next
        End If
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608747
replace Me.Text.Take with Me.textBox1.text
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 33608779
Hi jtoutou,

I still don't get it. Please see below description of Windows Form.

Header:

Total  99,999
Detail  :

Date   Qty
01       10
02       20
03       10
04       10
05       20
06       30

The user click Date 01, The total show 10. The left mouse still hold.
Then the user move mouse to row Date 03. While moving pass to date 02, the total show 30, and so on.

Using mouse move event?

Thank you.


0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 33608783
Now I know what you want to do. Try this:
Note: In order to do what you want, you need to set your datagridview's SelectMode = FullRowSelect and MultiSelect = True.

    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        Dim iTotal As Integer
        For Each row As DataGridViewRow In DataGridView1.SelectedRows
            iTotal += CInt(Val(row.Cells(1).FormattedValue))
        Next
        TextBox1.Text = iTotal

    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608818
try this...to get what you want you have to multi select the row headers...
Function SumColumn(ByVal dgv As DataGridView, ByVal columnIndex As Byte) As Double
        Dim total As Double = 0
        Try

            For Each row As DataGridViewRow In dgv.SelectedRows
                If Not row.IsNewRow Then
                    total += CType(row.Cells(columnIndex).Value, Double)
                End If
            Next
            Return total
        Catch ex As Exception
            Return 0
        End Try
    End Function
 Private Sub MYDataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MYDataGridView.SelectionChanged
        TextBox1.Text = SumColumn(MYDataGridView, 0)
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608838
The function works also with FullRowSelect
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608848
Also in your case the column index is 1
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33608873
@jtoutou, I don't see any difference between your code and mine....
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608876
i just create a function ..and i just finish it after your post..
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608878
Ok.....Emi @jtoutou 0 points Required!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 33608915
You mean you don't have point ?

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608921
Zhaolai post solution first...
just take a look the function ...you can call it just with one line
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 33608933
Ok.

Thank you very much for the function, it helps a lot, since several forms need it.

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 33608942
Hi Zhaolai,

Thank you for the solution.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33608955
<<Thank you very much for the function, it helps a lot, since several forms need it.>>
It seems that @jtoutou's comments also helped you. So you should split the points between him and me.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33608958
@Zhaolai  thank you very much ....it was my request for that ...no worries...I also learn for you ...You know that...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

705 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