Click here to Skip to main content
15,868,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i can sum of datagrid columns with for next loop
bu i use this all datagridviews different and again again write codes

can i change this loop like function or another thing?

What I have tried:

With dgv1
            ' tfi = .Rows(0).Cells(.Columns.Count - 1).Value
            For i = 0 To .Rows.Count - 2 : tfi = tfi + .Rows(i).Cells(.Columns.Count - 1).Value : Next

            .Rows(.Rows.Count - 1).Cells(2).Value = tfi
            .Rows(.Rows.Count - 1).Cells(0).Value = "G.Toplam"
            Dim f = New Font("Arial", 8, FontStyle.Bold)
            .Rows(.Rows.Count - 1).Cells(2).Style.Font = f
            .Rows(.Rows.Count - 1).Cells(0).Style.Font = f
            .Rows(.Rows.Count - 1).DefaultCellStyle.ForeColor = Color.Red
        End With
Posted
Updated 30-Nov-20 23:03pm
Comments
OriginalGriff 1-Dec-20 3:04am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 1-Dec-20 3:48am    
Create a function that accepts parameters: the datagridview reference, and the rows and columns to be summed.
Member 14588284 1-Dec-20 9:18am    
In the code I will write, I will only enter the name datagridview and the codes will look at the columns and sum the cells with numbers.

1 solution

I would suggest you to build your own dgv which inherits DataGridView and which contains the Methods which are required by you. Here is an Example how it could be realized :

VB
Public Class myDGV
    Inherits DataGridView

    Sub Init() Handles Me.HandleCreated
       Columns.Add("C1", "")
        Columns.Add("C2", "")
        Columns.Add("C3", "")
        Columns.Add("C4", "")
        Rows.Add(1.5, 4, 9, 0.25)
        Rows.Add(2.5, 2.5, 2.5, 1)
        Rows.Add(2.0, 3.5, -2.5, 1.5)
    End Sub




    ReadOnly Property RowSum As Single()
        Get
             Dim Sum(Rows.Count - 1) As Single
            For i As Integer = 0 To Columns.Count - 1
                For j As Integer = 0 To Rows.Count - 1
                    Sum(j) += CSng(Rows(j).Cells(i).Value)
                Next
            Next
            Return Sum
        End Get
    End Property

    ReadOnly Property ColumnSum As Single()
        Get
            Dim Sum(Columns.Count - 1) As Single
            For i As Integer = 0 To Rows.Count - 1
                For j As Integer = 0 To Columns.Count - 1
                    Sum(j) += CSng(Rows(i).Cells(j).Value)
                Next
            Next
            Return Sum
        End Get
    End Property

    ReadOnly Property GridSum As Single
        Get
            Dim Sum As Single = 0.0
            For i As Integer = 0 To Rows.Count - 1
                For j As Integer = 0 To Columns.Count - 1
                    Sum += CSng(Rows(i).Cells(j).Value)
                    'If IsNumeric(Rows(i).Cells(j).ValueType) Then Sum += CSng(Rows(i).Cells(j).Value)
                Next
            Next
            Return Sum
        End Get
    End Property

End Class


The Method Init at the beginnig is only for testing and to fill the DGV with some values ...
 
Share this answer
 
v2
Comments
Ralf Meier 1-Dec-20 7:04am    
Thanks a lot for upvoting my Solution ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900