Click here to Skip to main content
16,022,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a datagridview that contains the column "Amount".
I also have a textbox
I want the sum of the column "amount" to appear in the textbox
I have tried the following:

VB Code:
Dim total As Integer

For Each row As DataGridViewRow In DataGridView1.Rows
total += row.Cells(amount).Value
Next

txtmon.Text = total

I get the message "Cant find column "amount".
Parametername: ColumnName

Do you guys have an idea what i am doing wrong????

Thanks in advance
Sucharitha
Posted

total += row.Cells(amount).Value

correct this line

if your column name is 'amount' then you should write it in double quotes
total += row.Cells("amount").Value


or you can access column 'amount' by number start counting columns from 0 to onwards

eg.
ColumnName
name   address   amount
ColumnIndex
0      1         2



here, for 'amount' columnName, column-index is 2

so, we can write it as below
VB
total += row.Cells(2).Value 


Happy Coding!
:)
 
Share this answer
 
If you have column "Amount", then use the same name in your statement:
VB
total += row.Cells("Amount").Value

Or you can use variable 'amount':
VB
Dim amount As String="Amount"
For Each row As DataGridViewRow In DataGridView1.Rows
total += row.Cells(amount).Value
Next
 
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