?
Solved

Updating a textbox based on a new DataGridView row cell value

Posted on 2006-03-31
6
Medium Priority
?
2,560 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?  
0
Comment
Question by:purfus
  • 3
  • 3
6 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 16346996
If cell IsNot Nothing AndAlso Not cell.Value.Equals(System.DBNull.Value) Then

Bob
0
 
LVL 3

Author Comment

by:purfus
ID: 16347568
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16349536
How are you adding the row?  Typing in the blank row at the end of the DataGridView?

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:purfus
ID: 16350195
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16354373
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
 
LVL 3

Author Comment

by:purfus
ID: 16354672
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question