FMabey
asked on
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(ByVa l sender As System.Object, ByVal e As System.EventArgs)
Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingCon text(Me.DG _platesNEW .DataSourc e, 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).Colum ns.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
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(ByVa
Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingCon
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")
For x As Integer = 1 To DS_plates1.Tables(0).Colum
dr2.Item(x) = dr.Item(x)
Next
dr2.Item("PLT_ID") = STR_pltid
DS_plates1.Tables(0).Rows.
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
ASKER
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?
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.
How would I do that with your option?
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
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
ASKER
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).NewRo w
For x As Integer = 1 To DS_plates1.Tables(0).Colum ns.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.
Dim dr2 As DataRow
dr2 = DS_plates1.Tables(0).NewRo
For x As Integer = 1 To DS_plates1.Tables(0).Colum
dr2.Item(x) = dr.Item(x)
Next
dr2.Item("PLT_ID") = STR_pltid
DS_plates1.Tables(0).Rows.
The line dr2.Item("PLT_ID") = STR_pltid is inserting a new primary key to avoid problems with unique constraints.
ASKER
I don't understand why this code worked fine on a datagrid in VS2003 but doesn't ork for a datagridview in 2005.
What is the problem that you are having with that code?
Bob
Bob
ASKER
It is failing on the line:
dr2.Item(x) = dr.Item(x)
saying that I have a NullRefernceException.
dr2.Item(x) = dr.Item(x)
saying that I have a NullRefernceException.
Try this:
If Not IsDBNull(dr(x)) Then dr2(x) = dr(x)
Bob
If Not IsDBNull(dr(x)) Then dr2(x) = dr(x)
Bob
ASKER
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?
Where is 'dr' defined and set?
Bob
Bob
ASKER
This is my complete code... Including the declaration of dr which occurs at the top:
Dim dr As DataRow
Private Sub dg_CurrentCellChanged(ByVa l sender As System.Object, ByVal e As System.EventArgs)
Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingCon text(Me.DG _platesNEW .DataSourc e, 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).NewRo w
For x As Integer = 1 To DS_plates1.Tables(0).Colum ns.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?
Dim dr As DataRow
Private Sub dg_CurrentCellChanged(ByVa
Dim bm As BindingManagerBase = Me.DG_platesNEW.BindingCon
If bm.Position < 0 Then Exit Sub
dr = CType(bm.Current, DataRowView).Row
End Sub
Dim dr2 As DataRow
dr2 = DS_plates1.Tables(0).NewRo
For x As Integer = 1 To DS_plates1.Tables(0).Colum
dr2(x) = dr(x)
Next
dr2.Item("PLT_ID") = STR_pltid
DS_plates1.Tables(0).Rows.
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?
So 'dr' is only set when a cell is dirty?
Bob
Bob
ASKER
Dirty? Can you explain what that means? I've never come across that before!
So, 'dr' is only set when the value for a cell has changed. "dirty" = uncommitted.
Bob
Bob
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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
Bob
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
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")
Roger
ASKER
Thank you for your time on this Bob. It is much appreciated. RowEnter worksa treat.
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