DataGridView and sorting problem

K

Kyote

On my form I have a datagridview that's bound to a table on my DB.
When I run the app then click on a column header to sort, it sorts
just fine. But my problem is a result of that sort.

Even though the view has been sorted, it's underlying data isn't.
After sorting I'm trying to get the datarow I just doubleclicked on.
The problem is it's returning the pre sorted row. That is, the row
that was there before I sorted the column.

Dim temp As Integer = MembershipDataGridView.CurrentRow.Index
Dim mydatarow As DataRow
mydatarow = MembershiptextDataSet.Membership.Rows.Item(temp)
MessageBox.Show(mydatarow.Item("ID"))


I hope I explained the problem correctly. Can anyone help me?
 
C

Cor Ligthert[MVP]

Kyote,

If you use the dataview and the currencymanager to bind to your
datagridview, all will probably be correct.

Some samples of the currencymanager. We don't have it for a datagridview at
the moment.
http://www.vb-tips.com/dbpages.aspx?Search=currencymanager

A datarow from a dataview you get as (typed here, not tested so watch
writting errors)

dim dr as datarow = dv(currentrow).datarow

Cor
 
J

Jack Jackson

On my form I have a datagridview that's bound to a table on my DB.
When I run the app then click on a column header to sort, it sorts
just fine. But my problem is a result of that sort.

Even though the view has been sorted, it's underlying data isn't.
After sorting I'm trying to get the datarow I just doubleclicked on.
The problem is it's returning the pre sorted row. That is, the row
that was there before I sorted the column.

Dim temp As Integer = MembershipDataGridView.CurrentRow.Index
Dim mydatarow As DataRow
mydatarow = MembershiptextDataSet.Membership.Rows.Item(temp)
MessageBox.Show(mydatarow.Item("ID"))


I hope I explained the problem correctly. Can anyone help me?

When you sort, you are sorting the DataView that sits between your
DataTable and the DataGridView. The DataTable is never sorted. You
need to look at the DataView not the DataTable.

Normally you will be using the default DataView, which you can get
from DataTable.DefaultView.
 
K

Kyote

When you sort, you are sorting the DataView that sits between your
DataTable and the DataGridView. The DataTable is never sorted. You
need to look at the DataView not the DataTable.

Normally you will be using the default DataView, which you can get
from DataTable.DefaultView.

Thank you Jack. How would I go about accessing the dataview that's
part of the datagridview?
 
K

Kyote

Thank you Cor. I set the Datasource of the DGV to
MembershipBindingSource and the DataMember to Membership table. I'm
not sure what you mean but I'm looking up DataView and hopefully that
will shed some light. As for the CurrencyManager... I'm not sure I
understand. From what I seen it will allow me to determine the total
number of rows in a datagrid. But I can do that, it's just that that
isn't what I'm needing to accomplish.
If you use the dataview and the currencymanager to bind to your
datagridview, all will probably be correct.

Some samples of the currencymanager. We don't have it for a datagridview at
the moment.
http://www.vb-tips.com/dbpages.aspx?Search=currencymanager

A datarow from a dataview you get as (typed here, not tested so watch
writting errors)

dim dr as datarow = dv(currentrow).datarow

This looks promising. This is why I'm going to look into DataView.
 
K

Kyote

Mate, since you and Jack both mentioned DataView, I looked into it. I
then created a DV and used it for my DataSource for my DGV. Then using
this

Private Sub MembershipDataGridView_DoubleClick(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
MembershipDataGridView.DoubleClick
Dim temp As Integer = MembershipDataGridView.CurrentRow.Index
Dim mydatarow As DataRow
Dim x As Integer = 0
mydatarow = custDV.Item(temp).Row
For Each row As DataRowView In
MembershipBindingNavigator.BindingSource
If row.Item("ID") = mydatarow.Item("ID") Then
MembershipBindingSource1.Position = x
Exit For
End If
x = x + 1
Next
TabControl1.SelectTab(2)
End Sub

I now get the result I was looking for. If I sort the DGV by clicking
on a column and then double click a cell it brings up the correct
record just fine. Thank you for your help. If you still want to answer
my other reply, I will read it and possibly change things to suit.
Thank you again for all the help.
 
K

Kyote

Mate, thank you very much for the reply. I was able to solve the
problem because both you and Cor mentioned the DataView. Please read
my replies to him for further information.

I'd still very much appreciate it if you could answer my last reply to
your post though.

Thank you very much for helping me solve this problem.
 
J

Jack Jackson

Thank you Jack. How would I go about accessing the dataview that's
part of the datagridview?

See the last sentence of my previous post. Normally I would expect
the DataView that is being used to be the default one for the
DataTable.
 
G

Guest

' Handles sorted datagridview columns...

Private Sub DataGridViewTWoTime_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridViewTWoTime.CellClick
editSortSafe(TryCast(sender, DataGridView), e.RowIndex)
End Sub

Private Sub editSortSafe(ByRef dgridv As DataGridView, _
ByVal rowIndex As Integer)

Dim dtable As DataTable = TryCast(dgridv.DataSource, DataTable)
' Dim dtable As DataTable
Dim dv As DataView = dtable.DefaultView ' DataView is what is sorted!
Dim row As DataRow
Dim col As DataColumn
' Dim temp As Integer = dgridv.CurrentRow.Index
' row = dv.Item(temp).Row
row = dv.Item(rowIndex).Row
.. . .
 

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