Results 1 to 8 of 8

Thread: Sum a DataGrid column in a textboxt

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

    Sum a DataGrid column in a textboxt

    I need to sum the data from a DataGrid colum (W) into a textbox below the Datagrid (textbox3.text = SUM([W]) ).

    For this I have tried to use:
    Code:
    Me.TextBox3.Text = VBTestDataSet.Invoices.Compute("sum(W)", String.Empty)
    But it generate the erorr: "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 don't know what to do...

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Sum a DataGrid column in a textboxt

    Where exactly is that code located? The error message suggests that it is in an event handler that is invoked as a result of something that you did in the designer. You may have to add code to ensure that that line is only executed as a result of a change made by the user.

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

    Re: Sum a DataGrid column in a textboxt

    Code:
    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Me.InvoicesBindingSource.Filter = "CID ='" & ComboBox1.SelectedValue & "'"     
            Me.DataGridView1.Visible = True   
            Me.TextBox3.Text = VBTestDataSet.Invoices.Compute("sum(W)", String.Empty)
        End Sub

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

    Re: Sum a DataGrid column in a textboxt

    First things first, didn't we establish in another thread that your CID column contained numeric data and that you do not wrap numbers in single quotes in SQL code?

    As for the issue, it is as I said. An item is being selected in your ComboBox during the initialisation of the form that causes that event to be raised before any data has been added to that DataTable. The thing is, that's bad code no matter what. It shouldn't matter whether a number or NULL is returned because you're assigning the result to a String property so you should be assigning a String. Compute returns an Object reference, not a String, so you should be converting it to a String, which you would do by calling ToString on the object. If you set Option Strict On, which all VB.NET developers should do, then the code you have would not even compile because of the incompatible types you're trying to use.

    On a final note, using names like ComboBox1 and Textbox3 is very bad for all but the most rudimentary test or demo projects. For anything else you should be using meaningful names for everything, which means changing the name of every control as soon as you add it to the form.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Sum a DataGrid column in a textboxt

    First things first, didn't we establish in another thread that your CID column contained numeric data and that you do not wrap numbers in single quotes in SQL code?

    As for the issue, it is as I said. An item is being selected in your ComboBox during the initialisation of the form that causes that event to be raised before any data has been added to that DataTable. The thing is, that's bad code no matter what. It shouldn't matter whether a number or NULL is returned because you're assigning the result to a String property so you should be assigning a String. Compute returns an Object reference, not a String, so you should be converting it to a String, which you would do by calling ToString on the object. If you set Option Strict On, which all VB.NET developers should do, then the code you have would not even compile because of the incompatible types you're trying to use.

    On a final note, using names like ComboBox1 and Textbox3 is very bad for all but the most rudimentary test or demo projects. For anything else you should be using meaningful names for everything, which means changing the name of every control as soon as you add it to the form.

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

    Re: Sum a DataGrid column in a textboxt

    jmcilhinney, I 'm not a VB2010 developer, I just try to migrate from Access to VB.net ...and everything is so different(my code is only a rudimentary test to understand how VB.net works ). Also, I'm not an advanced speaker of English and I find it hard to understand all the explanations (especially when are not accompanied by examples).

    You said that "CID column contained numeric data and that you do not wrap numbers in single quotes in SQL code"... Where? In that code:
    Me.InvoicesBindingSource.Filter = "CID ='" & ComboBox1.SelectedValue & "'"
    ....but this is the only way I could make it work. If I remove the quote ("CID ='" to become CID=) , it will generate an error ...

    So, How do I sum in a textbox (below the datagrid), the column from a datagrid filtered by a combobox?

    In Access form is so easy...
    Last edited by Retelist; Aug 31st, 2012 at 01:10 AM.

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

    Re: Sum a DataGrid column in a textboxt

    I ' vs solve it. If somebody will need that, i'll post the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Beep()
    Dim sum As Integer = 0
    For i = 0 To Me.DataGridView1.RowCount - 1
    If Not DataGridView1.Rows(i).Cells(5).Value Is Nothing Then
    sum += Me.DataGridView1.Rows(i).Cells(5).Value.ToString()
    End If

    Next
    Me.TextBox3.Text = sum
    "Cells(5)" is the Column number... in fact the right column is 6 (5+1)

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

    Re: Sum a DataGrid column in a textboxt

    I ' vs solve it. If somebody will need that, i'll post the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Beep()
    Dim sum As Integer = 0
    For i = 0 To Me.DataGridView1.RowCount - 1
    If Not DataGridView1.Rows(i).Cells(5).Value Is Nothing Then
    sum += Me.DataGridView1.Rows(i).Cells(5).Value.ToString()
    End If

    Next
    Me.TextBox3.Text = sum
    "Cells(5)" is the Column number... in fact the right column is 6 (5+1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •