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

DataGridView and sorting problem

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

---
Kyote
Aug 1 '07 #1
Share this Question
Share on Google+
8 Replies


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

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

dim dr as datarow = dv(currentrow).datarow

Cor

"Kyote" <ky********@nospamhotmail.comschreef in bericht
news:la********************************@4ax.com...
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?

---
Kyote
Aug 2 '07 #2

P: n/a
On Wed, 01 Aug 2007 12:36:09 -0500, Kyote
<ky********@nospamhotmail.comwrote:
>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?

---
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.
Aug 2 '07 #3

P: n/a
On Wed, 01 Aug 2007 22:14:47 -0700, Jack Jackson
<ja********@pebbleridge.comwrote:
>On Wed, 01 Aug 2007 12:36:09 -0500, Kyote
<ky********@nospamhotmail.comwrote:
>>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?

---
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?

---
Kyote
Aug 2 '07 #4

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

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.
>
Cor

---
Kyote
Aug 2 '07 #5

P: n/a
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.
---
Kyote
Aug 2 '07 #6

P: n/a
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.
---
Kyote
Aug 2 '07 #7

P: n/a
On Thu, 02 Aug 2007 09:28:25 -0500, Kyote
<ky********@nospamhotmail.comwrote:
>On Wed, 01 Aug 2007 22:14:47 -0700, Jack Jackson
<ja********@pebbleridge.comwrote:
>>On Wed, 01 Aug 2007 12:36:09 -0500, Kyote
<ky********@nospamhotmail.comwrote:
>>>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?

---
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?

---
Kyote
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.
Aug 2 '07 #8

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

Oct 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.