Reputation: 1
I'm currently stuck on inserting a value into a row in datagridview
in vb.net
My code is:
Dim subtotal As Integer = Val(adult.Text) * 500 + Val(child.Text) * 300
For Each row As DataGridViewRow In DataGridView2.Rows
row.Cells.Insert(subtotal)
Next
It supposed to be like this. enter image description here
Upvotes: 0
Views: 729
Reputation: 54477
You don't insert a new cell. You get an existing cell in the appropriate column and set its Value property. This line:
row.Cells.Insert(subtotal)
would be like this:
row.Cells(columnIndex).Value = subtotal
That said, that will put the same value in every row. That's probably not what you want but it's all we can you based on the information you've provided. If what you actually expect is to get a different value for each row based on data in that row then the calculation would have to be inside the loop as well, e.g.
Dim adultCount = CInt(row.Cells(adultColumnIndex).Value)
Dim childCount = CInt(row.Cells(childColumnIndex).Value)
Dim subtotal = adultCount * 500 + childCount * 300
row.Cells(subtotalColumnIndex).Value = subtotal
Of course, if you had bound this grid to a DataTable
then you could set the Expression
property of the DataColumn
for the subtotal and it would be calculated automatically.
Upvotes: 1