mgmhicks
asked on
how to get to underlying datarow from datagridview
I have a windows form that has a bound datagridview, which also has a checkbox column that is not bound. I run through the gridview and get each row that is checked. What I need to do then is to get to the underlying datatable that corresponds to this datagridview row. The dataset table has been filtered before being bound to the datagrid.
thanks
thanks
ASKER
For i = 0 To gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0). FormattedV alue.ToStr ing = "True" Then
Dim myRow As DataRow = dsRoomItems.Tables(1).Sele ct("ID=" & gvItems2.Rows(i).Cells(1). Value.ToSt ring)
Else
End If
Next
getting "Value of type '-1-dimensional array of system.data.datarow' cannot be converted to 'system.data.datarow'
any Ideas?
If gvItems2.Rows(i).Cells(0).
Dim myRow As DataRow = dsRoomItems.Tables(1).Sele
Else
End If
Next
getting "Value of type '-1-dimensional array of system.data.datarow' cannot be converted to 'system.data.datarow'
any Ideas?
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, almost there. Here is the code I am running. I am trying to move selected items in grid2 to items in grid1 then remove the items in grid2. Not quite working leaving the 2nd item in the 2nd grid. How can I change. Thanks
Dim i As Integer = 0
Dim mystr() As String
Dim myRow As DataRow()
For i = 0 To gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0). FormattedV alue.ToStr ing = "True" Then
myrow = dsRoomItems.Tables(1).Sele ct("ID=" & gvItems2.Rows(i).Cells(1). Value.ToSt ring)
dsRoomItems.Tables(0).Impo rtRow(myRo w(0))
'dsRoomItems.Tables(1).Row s.Remove(m yRow(0))
Else
End If
Next
Dim mRow As DataGridViewRow
For Each mRow In gvItems2.Rows
If mRow.Cells(0).FormattedVal ue.ToStrin g = "True" Then
myRow = dsRoomItems.Tables(1).Sele ct("ID=" & mRow.Cells(1).Value.ToStri ng)
'dsRoomItems.Tables(0).Imp ortRow(myR ow(0))
dsRoomItems.Tables(1).Rows .Remove(my Row(0))
Else
End If
Next
Dim i As Integer = 0
Dim mystr() As String
Dim myRow As DataRow()
For i = 0 To gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0).
myrow = dsRoomItems.Tables(1).Sele
dsRoomItems.Tables(0).Impo
'dsRoomItems.Tables(1).Row
Else
End If
Next
Dim mRow As DataGridViewRow
For Each mRow In gvItems2.Rows
If mRow.Cells(0).FormattedVal
myRow = dsRoomItems.Tables(1).Sele
'dsRoomItems.Tables(0).Imp
dsRoomItems.Tables(1).Rows
Else
End If
Next
ASKER
i receive this error trying to remove the selected items from the grid view:
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
Here is the code:
For i = 0 To gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0). FormattedV alue.ToStr ing = "True" Then
myRow = dsRoomItems.Tables(1).Defa ultView.It em(i).Row
myRow.Delete()
i = i - 1
Else
End If
Next
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
Here is the code:
For i = 0 To gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0).
myRow = dsRoomItems.Tables(1).Defa
myRow.Delete()
i = i - 1
Else
End If
Next
It can be any of the indexed items such as rows, cells, datatables that you are accessing using index. Do you have 2 DataTables inside the dsRoomItems dataset?
ASKER
yes, so I select from grid2 copy to grid1 then want to remove from grid2, dsroomitems has 2 tables 1 to grid1 and 2 to grid 2
Try following
For i = 0 to gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0). FormattedV alue.ToStr ing = "True" Then
myRow = dsRoomItems.Tables(1).Defa ultView.It em(i).Row
myRow.Delete()
Else
End If
Next
i is automatically decremented in a for loop.
For i = 0 to gvItems2.Rows.Count - 1
If gvItems2.Rows(i).Cells(0).
myRow = dsRoomItems.Tables(1).Defa
myRow.Delete()
Else
End If
Next
i is automatically decremented in a for loop.
ASKER
CodeCruiser that is what I was trying before I added the I = I-1 what I think is going on is that indexed or rows.count get messed up once I remove the row from the dataset. Will removing it from grid instead take care of the underlying table?
Try following then
For i = gvItems2.Rows.Count - 1 To 0 Step -1
If gvItems2.Rows(i).Cells(0). FormattedV alue.ToStr ing = "True" Then
myRow = dsRoomItems.Tables(1).Defa ultView.It em(i).Row
myRow.Delete()
Else
End If
Next
For i = gvItems2.Rows.Count - 1 To 0 Step -1
If gvItems2.Rows(i).Cells(0).
myRow = dsRoomItems.Tables(1).Defa
myRow.Delete()
Else
End If
Next
ASKER
this code sends both selected items over to grid1 however, grid 2 only get rid of 1 item, the 2nd in the checkedlist items, and leaves it checked. Before the change both items would go over then leave the 2nd one in grid2. Has got to be something with the index changing in datatable after removing an item.
But still getting same index error.
But still getting same index error.
Index does change after deleting a row but that is why we are starting at the end and progressing to start so that index change does not affect us.
ASKER
looks like it is working now. This seems a lot harder than what it is on a web form and datagrid, but I think we got it. Not sure why it would be so hard, maybe it would be easier if I bound that column. Thank you very much. Your the best!!
Glad to help. I find winforms easier than webforms though :-)
Dim drow As DataRow() = dTable.Select("PrimaryColu