Datagrid event before Sort?

S

shuckjunkmail

It was suggested that I re-submit this question as a new post rather
than adding onto an old and unanswered post.

The basic problem has to do with the .NET datagrid and sorting. I am
having trouble getting the correct row of data from the datagrid
immediately after re-sorting the datagrid. I am using the dataview for
accessing the data and am using the following code as previously
mentioned in a different post:

Dim bm As BindingManagerBase = grdBaseRates.BindingContext( _
grdBaseRates.DataSource, _
grdBaseRates.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row

I'm currently handling the "click" event in my code and this gets
raised every time I click on the header of a column to re-sort the
data. However, this event seems to get raised BEFORE the grid has been
re-sorted. Thus, I sort by column 1, the event is raised, I handle the
event in my code but get the row associated with my initial form load
sort. Then I sort by column 2, the event is raised and I get the row
associated with the sort by column 1. I sort by column 3, the event is
raised and I get the row associated with the sort by column 3. etc. I'm
always 1 step behind because the event seems to happen before the grid
has been re-sorted.

Is there an event I can handle that happens AFTER the sort? Is
there another way to handle this?
Thanks in advance,
-Mike
Risetime Consulting
 
K

Ken Tucker [MVP]

Hi,


Get current row in datagrid. DS is a dataset.
Dim drv As DataRowView =
ds.Tables(0).DefaultView.Item(DataGrid1.CurrentCell.RowNumber)



Sort event.

Add a handler to the dataviews list changed event. If you are using
a datatable as the datasource use the datatable's defaultview list changed
event.

Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da, daEmployees As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

daEmployees = New OleDbDataAdapter("Select * From Employees Order by
LastName, FirstName", conn)

daEmployees.Fill(ds, "Employees")

DataGrid1.DataSource = ds.Tables("Categories")

DataGrid2.DataSource = ds.Tables("Employees")

AddHandler ds.Tables("Employees").DefaultView.ListChanged, AddressOf
ListChanged

End Sub



Private Sub ListChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.ListChangedEventArgs)

Dim hti As DataGrid.HitTestInfo

Dim pt As Point

pt = DataGrid2.PointToClient(Me.MousePosition)

hti = DataGrid2.HitTest(pt)

Trace.WriteLine(String.Format("Sort on column {0}", hti.Column))

End Sub



Ken

------------------------


It was suggested that I re-submit this question as a new post rather
than adding onto an old and unanswered post.

The basic problem has to do with the .NET datagrid and sorting. I am
having trouble getting the correct row of data from the datagrid
immediately after re-sorting the datagrid. I am using the dataview for
accessing the data and am using the following code as previously
mentioned in a different post:

Dim bm As BindingManagerBase = grdBaseRates.BindingContext( _
grdBaseRates.DataSource, _
grdBaseRates.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row

I'm currently handling the "click" event in my code and this gets
raised every time I click on the header of a column to re-sort the
data. However, this event seems to get raised BEFORE the grid has been
re-sorted. Thus, I sort by column 1, the event is raised, I handle the
event in my code but get the row associated with my initial form load
sort. Then I sort by column 2, the event is raised and I get the row
associated with the sort by column 1. I sort by column 3, the event is
raised and I get the row associated with the sort by column 3. etc. I'm
always 1 step behind because the event seems to happen before the grid
has been re-sorted.

Is there an event I can handle that happens AFTER the sort? Is
there another way to handle this?
Thanks in advance,
-Mike
Risetime Consulting
 

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