• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Error using Datagrid code on a datagridview

I'm using VS2005 (VB.NET)

I use the following code to copy a selected row into a new row in the same datagrid. However, when I try to apply the very same code to a datagridview it fails on: dr2.Item(x) = dr.Item(x)

My code is:

    Dim dr As DataRow

    Private Sub dg_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingContext(Me.DG_platesNEW.DataSource, Me.DG_platesNEW.DataMember)
        If bm.Position < 0 Then Exit Sub
        dr = CType(bm.Current, DataRowView).Row

    End Sub

Private Sub BTN_copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_copy.Click

Dim dr2 As DataRow
            dr2 = DS_plates1.Tables("PLATE").NewRow
            For x As Integer = 1 To DS_plates1.Tables(0).Columns.Count - 1
                dr2.Item(x) = dr.Item(x)
            Next
            dr2.Item("PLT_ID") = STR_pltid
            DS_plates1.Tables(0).Rows.Add(dr2)

end sub

The error highlights the dr2.Item(x) = dr.Item(x) line and says something about a NullRefernceException.

Could someone help me out here? I'm trying to convert all of my datagrids into datagridviews after an upgrade from VS2003 to 2005.

Cheers
0
FMabey
Asked:
FMabey
  • 10
  • 9
1 Solution
 
Bob LearnedCommented:
Try this:

Private Sub BTN_copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_copy.Click
            Dim dt1 As DataTable = DS_plates1.Tables("PLATE")
            Dim dt2 As DataTable = DS_plates1.Tables(0)
            For Each dr As DataRow In dt1.Rows
               dt2.ImportRow(dr)
            Next dr
End Sub

Bob
0
 
FMabeyAuthor Commented:
Cheers Bob,

Can you explain how that works? Also, I was using dr2 in my code to then use this piece of code...

            dr2.Item("PLT_ID") = STR_pltid
            DS_plates1.Tables(0).Rows.Add(dr2)

How would I do that with your option?
0
 
Bob LearnedCommented:
Modification:

Private Sub BTN_copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_copy.Click
            Dim dt1 As DataTable = DS_plates1.Tables("PLATE")
            Dim dt2 As DataTable = DS_plates1.Tables(0)
            For Each dr As DataRow In dt1.Rows
               dr("PLT_ID") = STR_pltid
               dt2.ImportRow(dr)
            Next dr
End Sub

You need to use ImportRow, because a DataRow cannot belong to 2 DataTables at the same time.

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
FMabeyAuthor Commented:
I only have one datatable. Sorry, maybe I confused things by posting some old code. This is my complete code:

Dim dr2 As DataRow
            dr2 = DS_plates1.Tables(0).NewRow
            For x As Integer = 1 To DS_plates1.Tables(0).Columns.Count - 1
                dr2.Item(x) = dr.Item(x)
            Next

            dr2.Item("PLT_ID") = STR_pltid
            DS_plates1.Tables(0).Rows.Add(dr)

The line dr2.Item("PLT_ID") = STR_pltid is inserting a new primary key to avoid problems with unique constraints.
0
 
FMabeyAuthor Commented:
I don't understand why this code worked fine on a datagrid in VS2003 but doesn't ork for a datagridview in 2005.
0
 
Bob LearnedCommented:
What is the problem that you are having with that code?

Bob
0
 
FMabeyAuthor Commented:
It is failing on the line:

dr2.Item(x) = dr.Item(x)

saying that I have a NullRefernceException.
0
 
Bob LearnedCommented:
Try this:

If Not IsDBNull(dr(x)) Then dr2(x) = dr(x)

Bob
0
 
FMabeyAuthor Commented:
Same error. I think it's a problem with 'dr'. It highlights the If Not IsDBNull(dr(x)) Then line so I'm thinking that maybe my dg_CurrentCellChanged sub is maybe not working?
0
 
Bob LearnedCommented:
Where is 'dr' defined and set?

Bob
0
 
FMabeyAuthor Commented:
This is my complete code... Including the declaration of dr which occurs at the top:

    Dim dr As DataRow

    Private Sub dg_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingContext(Me.DG_platesNEW.DataSource, Me.DG_platesNEW.DataMember)
        If bm.Position < 0 Then Exit Sub
        dr = CType(bm.Current, DataRowView).Row

    End Sub

Dim dr2 As DataRow
            dr2 = DS_plates1.Tables(0).NewRow
            For x As Integer = 1 To DS_plates1.Tables(0).Columns.Count - 1
                dr2(x) = dr(x)
            Next

            dr2.Item("PLT_ID") = STR_pltid
            DS_plates1.Tables(0).Rows.Add(dr2)
end sub

I have copied all into the button click sub and it works but it will not work if I keep the dg_CurrentCellChanged sud, which ideally I want as I will use it elsewhere.

Any ideas?
0
 
Bob LearnedCommented:
So 'dr' is only set when a cell is dirty?

Bob
0
 
FMabeyAuthor Commented:
Dirty? Can you explain what that means? I've never come across that before!
0
 
Bob LearnedCommented:
So, 'dr' is only set when the value for a cell has changed.  "dirty" = uncommitted.

Bob

0
 
FMabeyAuthor Commented:
It shouldn't be. It should be set when a row is selected. As I said, I thought I had this correct because it worked in VS2003 with datagrids. Do you think I need to change this to something else?
0
 
Bob LearnedCommented:
If 'dr' should be set when the row is selected, then I would put that code in the RowEnter event handler.

Bob
0
 
FMabeyAuthor Commented:
Ok,

Thanks for your help Bob. I will try that when I get back into work tomorrow and post how I get on. Strange that it used to work on the Datagrid in VS2003 though. I still have a copy and have just tried it again... It works on the datagrid in VS2003! Think this is wierd!

I'll post back tomorrow.
0
 
Bob LearnedCommented:
The DataGridView is a completely different beast, so that would make sense that it doesn't work like that DataGrid, and frankly I am happy that it doesn't.

Bob
0
 
SanclerCommented:
Bob

I don't think it has to be dirty.  It's the CurrentCellChanged Event, not CellValueChanged Event.  So any movement from cell to cell, whether there has been amendment in the vacated cell or not, should fire it.  But I still agree that RowEnter would be better: why keep re-capturing the same row if there's no movement out of the row?

FMabey

Although I don't particularly like the code, and I'm not sure of all the background details, I have tried it with what I think is the sort of setup intended and it works OK for me.  So I reckon the problem may be something to do with the set-up.  The only way I could reproduce a NullReferenceException in my test was by failing to Initialise my equivalent of your DS_plates1.Tables("PLATE") - and then it was not on the line you say.  I could generate other errors on the line you say (e.g. by having one less column in the target table than in the source table).  So I don't know exactly what the "set up" problem is.  But, as I say, the code itself (assuming a valid set up of the dataobjects) worked for me.

Roger
0
 
FMabeyAuthor Commented:
Thank you for your time on this Bob. It is much appreciated. RowEnter worksa  treat.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now