|
-
Nov 22nd, 2006, 02:23 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] VB2005 Sum column in datagridview
Hellowww again my friends...
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 TblBetalingAanMedewerkerDataGridView.Rows
Total += row.Cells("Bedrag").Value
Next
txtTotaalBedrag.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
-
Nov 22nd, 2006, 03:26 PM
#2
Re: VB2005 Sum column in datagridview
Do you see the column "Amount" on the datagridview?
You have
VB Code:
Total += row.Cells("Bedrag").Value
Obviously, "Bedrag" is not the same as "Amount"
-
Nov 22nd, 2006, 03:49 PM
#3
Re: VB2005 Sum column in datagridview
 Originally Posted by bodylojohn
Hellowww again my friends...
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 TblBetalingAanMedewerkerDataGridView.Rows
Total += row.Cells("Bedrag").Value
Next
txtTotaalBedrag.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
Hi,
You can try something like this;
VB Code:
TextBox1.Text = clumnName.Text ' put here your columnName
Hope it helps,
sparrow1
-
Nov 22nd, 2006, 04:14 PM
#4
Re: VB2005 Sum column in datagridview
I'll assume that you have this grid bound to a DataTable and say that you should use that table's Compute method:
VB Code:
'Sum the Bedrag column for all rows.
Me.TextBox1.Text = myDataTable.Compute("SUM(Bedrag)").ToString()
If the grid has been filtered then you can specify the filter as well, e.g.
VB Code:
'Sum the Bedrag column for all rows with an ID greater than 10.
Me.TextBox1.Text = myDataTable.Compute("SUM(Bedrag)", "ID > 10").ToString()
-
Nov 23rd, 2006, 02:03 AM
#5
Thread Starter
Fanatic Member
Re: VB2005 Sum column in datagridview
I translates "Bedrag" into "amount"
I have tried the following:
VB Code:
Dim table As DataTable
table = Me.Textielbedrijf2000DataSet.Tables("tblBetalingAanMedewerker")
Me.txtTotaalBedrag.Text = table.Compute("sum(Bedrag)")
But I get the message:
Argument not specified for parameter 'filter' of public function compute(expression as string, filter as string) as object
-
Nov 23rd, 2006, 02:04 AM
#6
Thread Starter
Fanatic Member
Re: VB2005 Sum column in datagridview
I forgot to say that it is part of a master detailform
So tblBetalingAanMedewerker is the child table of tblMedewerker
I hope that this is more detailed information
-
Nov 23rd, 2006, 02:05 AM
#7
Lively Member
Re: VB2005 Sum column in datagridview
VB Code:
Me.txtTotaalBedrag.Text = table.Compute("sum(Bedrag)","")
-
Nov 23rd, 2006, 02:38 AM
#8
Re: VB2005 Sum column in datagridview
Sorry. I wrote that code directly in the forum rather than pasting from an IDE and I was thinking that Compute was overloaded and the Filter was optional. lingsn is quite correct except you should never use an empty string literal:
VB Code:
Me.txtTotaalBedrag.Text = table.Compute("sum(Bedrag)", String.Empty)
-
Nov 24th, 2006, 05:06 AM
#9
Thread Starter
Fanatic Member
Re: VB2005 Sum column in datagridview
Thank you very very very much.
This works perfect.
Thanks again!!!
-
Dec 22nd, 2010, 08:27 AM
#10
New Member
Re: [RESOLVED] VB2005 Sum column in datagridview
Yeah i know this has been resolved, but I wanted to add something to this. it seems the resolution was to sum the datatable that was bound tothe dgv. However, if someone was needing to sum the columns of the dgv w/o messing w/ datatable, use the following:
Dim sum As Double = 0
For i = 0 To dgv.RowCount - 1
sum += dgv.Rows(i).Cells("ColumnName").Value()
Next
lbl.text = sum
-
May 19th, 2011, 02:52 AM
#11
New Member
Re: [RESOLVED] VB2005*-vb-2008 Sum column in datagridview
thanks! it also works in vb2008.
-
Aug 30th, 2012, 01:05 PM
#12
Junior Member
Re: [RESOLVED] VB2005 Sum column in datagridview
I can't manage to work in VB2010...
Me.TextBox3.Text = VBTestDataSet.Invoices.Compute("sum(W)", String.Empty)
is not working; Error: "An error occurred creating the form. See Exception.InnerException for details. The error is: Conversion from type 'DBNull' to type 'String' is not valid."
Me.TextBox3.Text = Invoices.Compute("sum(W)", String.Empty)
is not working; Error : "An error occurred creating the form. See Exception.InnerException for details. The error is: Conversion from type 'DBNull' to type 'String' is not valid."
I must change <DBNull> to 0 ? Where ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|