Datagrid/Dataset bindings

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
'----
 
K

Kris Langohr[MSFT]

Rick,

I tried the code you supplied on both VS.Net 2002 and VS.Net 2003 and in
both cases the code did exactly what it is supposed to do based on your
description. The rows that I multi-select in the grid are removed from the
dataset and show up in the secondary table. In your experience what rows
are being deleted that shouldn't be.


Kris
VB Data QA Team

This posting is provided "AS IS" with no warranties and confers no rights.
 
J

Jeff Shelby

Rick said:
DataGrid1.Select(indexArray(j))
MsgBox("j:" & j & " indexArray(j):" &
indexArray(j) & " bm.Position:" & bm.Position)
bm.RemoveAt(bm.Position)

I think you are having problems because "Select" doesn't change the
current position in the grid so bm.Position probably isn't changing.
If you replace

DataGrid1.Select(indexArray(j))

with

DataGrid1.CurrentIndex = indexArray(j)

you might get better results.

HTH

-JLS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top