After DataGrid Sort find Current Row?

B

Becker

Very simple, I have a datagrid on a windows form. I load some rows in it.
I click a column header and it sorts ascending. I click it again it sorts
descending. This is great, but what I want to do is after the sort, I want
to know what row is now the "selected" row. I want to do this so I can
update some stuff on the form that correlates to this row. This does not
seem so easy...please advise.

Thanks,
Ben
 
B

Becker

Ken,

I'm confused. The example you pointed me to does the exact same thing I've
got now. When you click, it shows you the name of the current row pre-sort,
but when you let up and it sorts, it doesn't show the name of the post-sort
record that is selected.

What I want is after the sort, the row that is selected, I want to know it's
data values, not the record that used to be there before the sort. Make
sense? I've looked at an answer on syncfusion, and it uses the bindcast
thing, but it doesn't seem to work. I've not seen any working examples of
this. Any ideas?

Thanks,
Ben
 
C

Cor Ligthert

Hi Ben,

The first question, did you use the dataview as the datasource.

That solves the sort problem mostly, and than use the dataview as the
connection to your tables

I hope this helps?
(And when not reply)

Cor
 
K

Ken Tucker [MVP]

Hi,

Made a few changes I added a handler to the dataview list changed
event.

Changes to form load

DataGrid1.DataSource = ds.Tables("Categories")
DataGrid2.DataSource = ds.Tables("Employees")
AddHandler ds.Tables("Categories").DefaultView.ListChanged,
AddressOf OnListChanged

Other changes

Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(e.X, e.Y)

If hti.Type = DataGrid.HitTestType.ColumnHeader Then
'
' Just sorted
'

Trace.WriteLine(DataGrid1.TableStyles(0).GridColumnStyles.Item(hti.Column).MappingName)
End If
End Sub

Protected Sub OnListChanged(ByVal sender As Object, ByVal args As
System.ComponentModel.ListChangedEventArgs)
GetName()
End Sub

Private Sub GetName()
Dim cm As CurrencyManager =
CType(Me.BindingContext(DataGrid1.DataSource), _
CurrencyManager)
Dim drv As DataRowView

drv = ds.Tables("Categories").DefaultView.Item(cm.Position)
Trace.WriteLine(drv.Item("CategoryName").ToString)
End Sub

Ken
 
B

Becker

No, thats probably my problem. I use datasource = ds.tables("table")

I will try the ideas I've seen here.

Thanks!
Ben
 

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