We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Updating a textbox based on a new DataGridView row cell value

purfus
purfus asked
on
Medium Priority
2,576 Views
Last Modified: 2008-02-01
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?  
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
If cell IsNot Nothing AndAlso Not cell.Value.Equals(System.DBNull.Value) Then

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
How are you adding the row?  Typing in the blank row at the end of the DataGridView?

Bob

Author

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".
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.