Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

DataGridView and sorting problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kyote - 01 Aug 2007 18:36 GMT
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
Cor Ligthert[MVP] - 02 Aug 2007 04:09 GMT
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

> 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
[quoted text clipped - 14 lines]
> ---
> Kyote
Kyote - 02 Aug 2007 15:38 GMT
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.
[quoted text clipped - 7 lines]
>
>dim dr as datarow = dv(currentrow).datarow

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

>Cor

---
Kyote
Kyote - 02 Aug 2007 18:07 GMT
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
Jack Jackson - 02 Aug 2007 06:14 GMT
>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
[quoted text clipped - 14 lines]
>---
>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.
Kyote - 02 Aug 2007 15:28 GMT
>>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
[quoted text clipped - 21 lines]
>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
Jack Jackson - 02 Aug 2007 18:51 GMT
>>>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
[quoted text clipped - 27 lines]
>---
>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.
Kyote - 02 Aug 2007 18:09 GMT
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
dgesmi - 30 Oct 2007 20:58 GMT
' 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
. . .

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.