By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Datagrid event before Sort?

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,
Get current row in datagrid. DS is a dataset.
Dim drv As DataRowView =
ds.Tables(0).DefaultView.Item(DataGrid1.CurrentCel l.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

------------------------
<sh***********@sbcglobal.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Nov 21 '05 #2

P: n/a
Hi,
Get current row in datagrid. DS is a dataset.
Dim drv As DataRowView =
ds.Tables(0).DefaultView.Item(DataGrid1.CurrentCel l.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

------------------------
<sh***********@sbcglobal.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.