{Vb.net} Sum of Cells not Columns

Richard Neider 21 Reputation points
2022-11-18T08:27:06.65+00:00

I would have bet that there was a way to get the value as double from each cell between 1-6 (game 1 through game 6 in this situation) and have it added to the final cell.
I cannot find or remember anything to that effect. I can add rows under columns all day. But that's not what I need to do. Any Ideas?

This uses DataGridView and the numbers are manually added.

261786-image.png

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-11-18T09:55:10.84+00:00

    Hi @Richard Neider ,
    Use 'DataGridView1.AllowUserToAddRows = False' to prevent users from adding new rows.
    You can use the following code to sum the numbers.
    I put it in the CellEnter event to trigger the sum every time the cell is clicked.

        Private Sub DataGridView1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEnter  
            For Each ln As DataGridViewRow In DataGridView1.Rows  
                ln.Cells(7).Value = 2 * (Convert.ToInt32(ln.Cells(1).Value) + Convert.ToInt32(ln.Cells(2).Value) + Convert.ToInt32(ln.Cells(3).Value) + Convert.ToInt32(ln.Cells(4).Value) + Convert.ToInt32(ln.Cells(5).Value) + Convert.ToInt32(ln.Cells(6).Value))  
            Next  
        End Sub  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            DataGridView1.AllowUserToAddRows = False  
        End Sub  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.