Updating a textbox based on a new DataGridView row cell value

On a windows form I have a datagridview that allows the user to add new rows.  Initially the datagridview has no data in it and no rows.  A dataset is used to store the datagridview row values.  

The datagridview has 6 columns with the last column a textbox where an integer is entered.  The other columns are dropdown menus.   There is label on the form that holds the total of all of the integers entered in the 5th column of the datagridview.  When the user finishes entering the number in the last column I need to have all of the numbers in the last column totaled and then displayed in the label.


Here is the code that I'm trying to use to get the value from a new datagridview cell value and update the textbox with the total of the numbers:

Private Sub dgdSpecies_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgdSpecies.CellEndEdit
        Dim cell As DataGridViewCell = dgdSpecies(e.ColumnIndex, e.RowIndex)
        If cell.ColumnIndex = 5 Then
            Dim cellValue As String
            Dim i, count As Integer
            Dim total As Double
            total = 0
            count = dgdSpecies.RowCount
            For i = 0 To count - 1
                cell = dgdSpecies(e.ColumnIndex, i)
                If Not cell.Value.Equals(System.DBNull.Value) Then
                    cellValue = cell.Value
                    If Microsoft.VisualBasic.Information.IsNumeric(cellValue) Then
                        total = total + CDbl(cellValue)
                    Else
                        MessageBox.Show("You can only enter a number for the Species Price.  Do not enter the ""$"" sign.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                End If
            Next
            lblBidTotal.Text = String.Format("{0:n2}", total)
        End If
    End Sub

I keep getting a Null Reference Exception for the line: If Not cell.Value.Equals(System.DBNull.Value) Then
I know that it's because "cell" is not an instance of an object because the new datagridview row hasn't been committed yet.

How would I go about getting all of the values in the 6th column, including a new row, and totaling them and then displaying the total in a label?  
LVL 3
purfusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If cell IsNot Nothing AndAlso Not cell.Value.Equals(System.DBNull.Value) Then

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
purfusAuthor Commented:
That doesn't work to get around the eror, and that's not exactly the help that I need.  (Still getting the Null Reference Exception error)

I need to get the value of the cell of a new row in the datagridview while that new row is still being added and is not yet commited to the dataset.  Perhaps I need to commit the row to the dataset but I don't know how to do that.

Thank you for your help.
0
Bob LearnedCommented:
How are you adding the row?  Typing in the blank row at the end of the DataGridView?

Bob
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

purfusAuthor Commented:
Bob,

yes the user can add a new row to the datagridview by filling in information in a blank row at the end of the datagridview.  

I've noticed that the information from the new row isn't added to the dataset until the focus is taken off of that new row by either going to the next row or calling "dgv.currentcell = nothing".
0
Bob LearnedCommented:
Right ;)  But, you still should be able to get values from the DataGridView control for the uncommitted row, so I am confused as to what your problem is.

Bob
0
purfusAuthor Commented:
Bob,

Thanks for your help on this one.  I figured it out.  Your earlier reply was partially correct.  I added a test for "cell.value IsNot Nothing" and it works.  Here's the code that I'm using.

     Dim i, count As Integer
     count = dgv.RowCount
     For i = 0 To count - 1
       If cell IsNot Nothing AndAlso cell.Value IsNot Nothing Then
            If  Not cell.Value.Equals(System.DBNull.Value) Then
                If Microsoft.VisualBasic.Information.IsNumeric(cell.Value) Then
                    dbl = CDbl(cell.Value)
                Else
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        End If
   Next

The problem was that in the For Next loop when it got to the last row on the DataGridView which was always empty, cell.value was Nothing so before that was tested, it would also fail when trying to do " If  Not cell.Value.Equals(System.DBNull.Value) Then".  Of course, if I just did "For i = 0 To count - 2" instead of "For i = 0 To count - 1", that would work too.  

I appreciate your effort and help on this, and sense your answer was the inspiration for figuring this out, I'll give you the points anyways.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.