CurrentRow in Datagrid - always the first :-(

D

dr. p. dietz

Hello,
i'm near a depression by using the datagrid...

My Form has "only" one Datagrid 'dgProt',on form_load i
create a dataset 'ds' by first using a dataadapter and
second adding some calculated columns. This ds is the
datasource of the datagrid, datamember is the only one
table 'protokoll' in the dataset.

Me.dgProt.SetDataBinding(ds, "protokoll")

Ok, the grid shows the data. On doubleclick or select a
menuitem, i want to get the currentrow for further
reactions:
I've tried:
Dim x As DataRowView, Id, n
x = Me.BindingContext(ds, "protokoll").Current
Id = x!naqs_nr (the index-column i need)
or:
Dim cm As CurrencyManager = CType(Me.BindingContext
(Me.dgProt.DataSource, Me.dgProt.DataMember),
CurrencyManager)
Dim dv As DataView = CType(cm.List, DataView)
Dim x As DataRow
x = dv.Item(cm.Position).Row
id=x!naqs_nr
or:
Dim bm As BindingManagerBase = Me.BindingContext
(Me.dgProt.DataSource, Me.dgProt.DataMember)
x = CType(bm.Current, DataRowView).Row
Id = x!naqs_nr
------------
The first time, every thing is fine, but after changing
the dataset (e.g. repopulate it by ds.tables
(protokoll).clear and refill with dataadapter), or after
sorting it, i always get the first row. I'v tried to Re-
Bind the datagrid, different other possibilities, but no
effect...

Please help me!

Thanks
Peter
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello Peter,
The first time, every thing is fine, but after changing
the dataset (e.g. repopulate it by ds.tables
(protokoll).clear and refill with dataadapter), or after
sorting it, i always get the first row.

This behavior is most likely by design. I can confirm that I have
experienced the same thing so it is definitely not a bug in your code. One
solution I have managed to find is to store the current row's primary key
somewhere and look up the row again after the data grid has been re-sorted
or re-bound.

Another is to subscribe to the DataView's ListChanged event - and the
DataView is available from the CurrencyManager's List property. You should
then react on ItemMoved notifications and check oldIndex and newIndex - but
this won't work when a new dataset has been assigned to the data grid.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx

Bring the power of unit testing to VS .NET IDE
 

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