R
Rick
I'm trying to have a code removing selected rows in a
datagrid from the underlying dataset.
Because the DataGrid can be sorted the row numbers do not
match, so I wrote this code but for some reason it still
removing the wrong lines.
Any help will be greatly appreciated.
'----
Dim o As Object
Dim indexArray As New SortedList()
Dim bm As BindingManagerBase = BindingContext
(DataGrid1.DataSource, DataGrid1.DataMember)
Dim i As Integer
Dim j As Integer = 0
For i = 0 To DataGrid1.DataSource.rows.count - 1
If DataGrid1.IsSelected(i) Then
j += 1
indexArray.Add(j, i)
End If
Next
For j = indexArray.Count To 1 Step -1
DataGrid1.Select(indexArray(j))
MsgBox("j:" & j & " indexArray(j):" &
indexArray(j) & " bm.Position:" & bm.Position)
bm.RemoveAt(bm.Position)
Next
Dim dt As DataTable
dt = DataGrid1.DataSource.GetChanges()
DataGrid1.DataSource.AcceptChanges()
dt.RejectChanges()
DataGrid2.DataSource = dt
'----
datagrid from the underlying dataset.
Because the DataGrid can be sorted the row numbers do not
match, so I wrote this code but for some reason it still
removing the wrong lines.
Any help will be greatly appreciated.
'----
Dim o As Object
Dim indexArray As New SortedList()
Dim bm As BindingManagerBase = BindingContext
(DataGrid1.DataSource, DataGrid1.DataMember)
Dim i As Integer
Dim j As Integer = 0
For i = 0 To DataGrid1.DataSource.rows.count - 1
If DataGrid1.IsSelected(i) Then
j += 1
indexArray.Add(j, i)
End If
Next
For j = indexArray.Count To 1 Step -1
DataGrid1.Select(indexArray(j))
MsgBox("j:" & j & " indexArray(j):" &
indexArray(j) & " bm.Position:" & bm.Position)
bm.RemoveAt(bm.Position)
Next
Dim dt As DataTable
dt = DataGrid1.DataSource.GetChanges()
DataGrid1.DataSource.AcceptChanges()
dt.RejectChanges()
DataGrid2.DataSource = dt
'----