Click here to Skip to main content
15,746,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys how are you doing.

i was wondering about how to add rows in a data grid view.

So one row would equal one number and another row would equal another number and pressing a button would add both rows.

It might be wrong but this is the current code

Seat_PlanDataGridView.CurrentCell.Value = Button2.Text

and help is much apperciated
Updated 26-Mar-13 8:39am
Kschuler 26-Mar-13 16:02pm    
Do you mean add two rows as in "create another row in the grid" or add two rows as in "mathematically perform addition on the values contained in two rows"?
safianm 28-Mar-13 9:29am    

If you want to add row to Windows.Forms.DataGridView, see this:[^]
Share this answer
You've said you want to calculate the addition on two rows so....I'm not sure exactly what you are having trouble with, but to access the data in a row, you would access the row's cell's value something like this (Assuming that the data you want is in the first row and first column):

So if you had two rows in the grid and only one column you would add them together like this (Assuming that the cells contain decimal values):
Dim decAddedValue As Decimal = dgvGrid.Rows(0).Cells(0).Value + dgvGrid.Rows(1).Cells(0).Value

Your best bet would be to create a function that makes sure a value IS what you expect it to be and/or returns a converted value...for example, it may be possible to have a null value in your cell, in which case the above code will fail. I'd do something like this:

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    Dim decAddedValue As Decimal = CvtObjDec(dgvGrid.Rows(0).Cells(0).Value) + CvtObjDec(dgvGrid.Rows(1).Cells(0).Value)
    MsgBox("Value is: " & decAddedValue.ToString)
End Sub

Public Function CvtObjDec(ByVal obj As Object) As Decimal
   If obj Is DBNull.Value Then Return 0
   If obj Is Nothing Then Return 0
   If obj.ToString.Trim.Length = 0 Then Return 0
   If Not IsNumeric(obj.ToString) Then Return 0
   Return CDec(obj.ToString)
End Function
Share this answer

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