Results 1 to 12 of 12

Thread: [RESOLVED] VB2005 Sum column in datagridview

  1. #1
    Fanatic Member
    Join Date
    Jul 06
    Location
    Netherlands
    Posts
    734

    Resolved [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:
    1. dim total as Integer
    2.  
    3. For Each row As DataGridViewRow In  TblBetalingAanMedewerkerDataGridView.Rows
    4. Total += row.Cells("Bedrag").Value
    5. Next
    6.  
    7. 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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 06
    Location
    Providence, RI - USA
    Posts
    9,172

    Re: VB2005 Sum column in datagridview

    Do you see the column "Amount" on the datagridview?
    You have
    VB Code:
    1. Total += row.Cells("Bedrag").Value
    Obviously, "Bedrag" is not the same as "Amount"

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 05
    Location
    Globetrotter
    Posts
    2,817

    Re: VB2005 Sum column in datagridview

    Quote 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:
    1. dim total as Integer
    2.  
    3. For Each row As DataGridViewRow In  TblBetalingAanMedewerkerDataGridView.Rows
    4. Total += row.Cells("Bedrag").Value
    5. Next
    6.  
    7. 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:
    1. TextBox1.Text = clumnName.Text ' put here your columnName

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    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:
    1. 'Sum the Bedrag column for all rows.
    2. 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:
    1. 'Sum the Bedrag column for all rows with an ID greater than 10.
    2. Me.TextBox1.Text = myDataTable.Compute("SUM(Bedrag)", "ID > 10").ToString()

  5. #5
    Fanatic Member
    Join Date
    Jul 06
    Location
    Netherlands
    Posts
    734

    Re: VB2005 Sum column in datagridview

    I translates "Bedrag" into "amount"

    I have tried the following:

    VB Code:
    1. Dim table As DataTable
    2. table = Me.Textielbedrijf2000DataSet.Tables("tblBetalingAanMedewerker")
    3. 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

  6. #6
    Fanatic Member
    Join Date
    Jul 06
    Location
    Netherlands
    Posts
    734

    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

  7. #7
    Lively Member
    Join Date
    Nov 06
    Posts
    116

    Re: VB2005 Sum column in datagridview

    VB Code:
    1. Me.txtTotaalBedrag.Text = table.Compute("sum(Bedrag)","")

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    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:
    1. Me.txtTotaalBedrag.Text = table.Compute("sum(Bedrag)", String.Empty)

  9. #9
    Fanatic Member
    Join Date
    Jul 06
    Location
    Netherlands
    Posts
    734

    Re: VB2005 Sum column in datagridview

    Thank you very very very much.
    This works perfect.

    Thanks again!!!

  10. #10
    New Member
    Join Date
    Dec 10
    Posts
    1

    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

  11. #11
    New Member
    Join Date
    May 11
    Posts
    1

    Re: [RESOLVED] VB2005*-vb-2008 Sum column in datagridview

    thanks! it also works in vb2008.

  12. #12
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    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
  •