473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DataGridView and sorting problem

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
8 7412
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
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
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
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
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
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
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
' 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: rob | last post by:
I have a class that among others exposes a string property "Date". The date in this property is stored in the form yyyymmdd. Now I do the following 1) Generate a DataGridViewTextBoxColumn column...
4
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone...
4
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I...
0
by: mahesh.nimbalkar | last post by:
I want to use Multi column sorting in DataGridView. The scenario is like this: 1) DataGridView has two columns; Id and name 2) User clicks on Id column and DataGridView is sorted...
1
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using a DataGridView on a Form. I want to disable the clicking on the columns headers to disallow the sorting. How can I do that, but without deriving the DataGridView? Is there any other...
4
by: ReneMarxis | last post by:
Hello i have one problem with sorting in an unbound DataGridView. I set sorting to automatic but i get errors when having DBNull values in the column that gets sort ("Object must be of type...
6
by: Simon Harvey | last post by:
Hi all, I'm really hoping someone can help me with this as it's causing me some serious problems. I have a Windows Forms application using the gridview control. When the user selects a row,...
7
Plater
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen....
0
by: Queez | last post by:
OK, this is rediculous... Surely, sorting should be one of the easiest things with the ASP.NET DataGridView control. I mean, there's hundreds of sites out there (Microsoft MSDN entries included)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.