473,326 Members | 2,010 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,326 software developers and data experts.

Dataview will not sort

I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen
Nov 21 '05 #1
9 1204
The View sort and the "sort" on the table are two different things. Try
binding the DataView to some control in your application and looking at the
order rather than looping through the original rows.
---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Raymond Lewallen" wrote:
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen

Nov 21 '05 #2
It appears that it is not sorted because you are using For Each on the
DataTable instead of looping through the sorted rows in the DataView by
index

Try this instead:
For i As Integer = 0 To dv.Count - 1

Dim eachrow As DataRowView = dv.Item(i)

Console.WriteLine(String.Concat(eachrow.Item("A"). ToString(), " - ",
eachrow.Item("B").ToString()))

Next

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:Om**************@TK2MSFTNGP14.phx.gbl...
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never
had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on
each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen

Nov 21 '05 #3
Raymond,

Does this show it you?

\\\Using the original table from the dataview
For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.Concat(eachRow.Item("A"). ToString(),
"-", eachRow.Item("B").ToString()))
Next
///
\\\Using the dataview
For Each eachRow As DataRowView In dv
Console.WriteLine(String.Concat(eachRow.Item("A"). ToString(),
"-", eachRow.Item("B").ToString()))
Next
///

Cor

"Raymond Lewallen" <Ra******************@nospam.faa.gov>
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never
had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on
each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen

Nov 21 '05 #4
You could also use:

Dim iterator As IEnumerator = dv.GetEnumerator
While iterator.MoveNext

Dim drv As System.Data.DataRowView = CType(iterator.Current,
System.Data.DataRowView)

Console.WriteLine(String.Concat(drv.Item("A").ToSt ring(), " - ",
drv.Item("B").ToString()))

End While
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
It appears that it is not sorted because you are using For Each on the
DataTable instead of looping through the sorted rows in the DataView by
index

Try this instead:
For i As Integer = 0 To dv.Count - 1

Dim eachrow As DataRowView = dv.Item(i)

Console.WriteLine(String.Concat(eachrow.Item("A"). ToString(), " - ",
eachrow.Item("B").ToString()))

Next

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:Om**************@TK2MSFTNGP14.phx.gbl...
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never
had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on
each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen


Nov 21 '05 #5
The view sort and the sort on the base table are 2 different things, that's
all binding and looking at it will confirm, and you already told me that's
the case, and I trust you so no need to see it for myself. I need to know
the best way to take the sorted dataview and loop through it in sorted form
so my console.write will output sorted information. How can I get the base
table sorted? Probably can't. Will I have to do DataView.CopyTo to move
the contents into an array, sort the array and then pull out the
information? How are other people solving these issues?

--

Raymond Lewallen

"Cowboy (Gregory A. Beamer) - MVP" <No************@comcast.netNoSpamM> wrote
in message news:AA**********************************@microsof t.com...
The View sort and the "sort" on the table are two different things. Try
binding the DataView to some control in your application and looking at the order rather than looping through the original rows.
---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Raymond Lewallen" wrote:
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(), " - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never had this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on each column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen

Nov 21 '05 #6
That's what I was looking for. Thank you. I'm a library writer and don't
spend much time on these types that are generally used for controls and GUI.

--

Raymond Lewallen
http://rlewallen.blogspot.com

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:Or*************@TK2MSFTNGP14.phx.gbl...
You could also use:

Dim iterator As IEnumerator = dv.GetEnumerator
While iterator.MoveNext

Dim drv As System.Data.DataRowView = CType(iterator.Current,
System.Data.DataRowView)

Console.WriteLine(String.Concat(drv.Item("A").ToSt ring(), " - ",
drv.Item("B").ToString()))

End While
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
It appears that it is not sorted because you are using For Each on the
DataTable instead of looping through the sorted rows in the DataView by
index

Try this instead:
For i As Integer = 0 To dv.Count - 1

Dim eachrow As DataRowView = dv.Item(i)

Console.WriteLine(String.Concat(eachrow.Item("A"). ToString(), " - ",
eachrow.Item("B").ToString()))

Next

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message news:Om**************@TK2MSFTNGP14.phx.gbl...
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(), " - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never
had
this happen, and I've spent 6 hours trying to figure out why it will not work. I've even tried sorting on column "B", and tried sorting DESC on
each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen



Nov 21 '05 #7
Cor,

Yes, your solution does work. Thank you for posting it. Sorry to bother
everyone with what seems to be such a remedial question, but I spend most of
my time in the database and writing supporting libraries, so using these
types that are more common to the GUI and used to bind to controls is not
something I spend much time doing, and DataRowView doesn't come up in very
easily to find places when looking through the help on dataviews.

Thanks again to Cowboy, Jim and Cor.

--

Raymond Lewallen
http://rlewallen.blogspot.com

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Raymond,

Does this show it you?

\\\Using the original table from the dataview
For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.Concat(eachRow.Item("A"). ToString(),
"-", eachRow.Item("B").ToString()))
Next
///
\\\Using the dataview
For Each eachRow As DataRowView In dv
Console.WriteLine(String.Concat(eachRow.Item("A"). ToString(),
"-", eachRow.Item("B").ToString()))
Next
///

Cor

"Raymond Lewallen" <Ra******************@nospam.faa.gov>
I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never
had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on
each
column too. Nothing seems to work.

Any help is greatly appreciated.

--

Raymond Lewallen


Nov 21 '05 #8
Hi Raymond,

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
The view sort and the sort on the base table are 2 different things,
that's
all binding and looking at it will confirm, and you already told me that's
the case, and I trust you so no need to see it for myself. I need to know
the best way to take the sorted dataview and loop through it in sorted
form
so my console.write will output sorted information.
foreach (DataRowView rowView in view)
Console.WriteLine(rowView["Column"]);

How can I get the base table sorted? Probably can't. Will I have to do DataView.CopyTo to move
the contents into an array, sort the array and then pull out the
information? How are other people solving these issues?
Why do you need to sort table?
Why don't you just use DataView?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

--

Raymond Lewallen

"Cowboy (Gregory A. Beamer) - MVP" <No************@comcast.netNoSpamM>
wrote
in message news:AA**********************************@microsof t.com...
The View sort and the "sort" on the table are two different things. Try
binding the DataView to some control in your application and looking at

the
order rather than looping through the original rows.
---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Raymond Lewallen" wrote:
> I have a dataview in which the sort property will not sort the
> dataview.
> Here's is a simple scenario similar to what I am doing:
>
> Class Foo
>
> Private Function Retrieve() As DataView
> ' Returns a DataView with 2 columns and 3 rows
> Dim ADOHelper As New DAL.ADOHelper
> Return ADOHelper.GetMyDataview()
> End
>
> Public Sub ShowDataView()
> Dim dv As DataView = Retrieve()
> ' dv's table has 2 columns, integer called "A" and string called
> "B"
> ' dv has 3 rows as follows:
> ' 2 "DEF"
> ' 3 "GHI"
> ' 4 "JKL"
>
> Dim drow As DataRow
> drow = dv.Table.NewRow()
> drow("A") = 1
> drow("B") = "ABC"
> dv.Table.Rows.Add(drow)
>
> ' HERE IS THE PROBLEM. SORT DOES NOT SORT.
> dv.Sort = "A"
>
> For Each eachRow As DataRow In dv.Table.Rows
> Console.WriteLine(String.ConCat(eachRow.Item("A"). ToString(), > " - ", eachRow.Item("B").ToString()))
> Next
>
> End Class
>
> When calling Foo.ShowDataView(), the output will be the following:
>
> 2 - DEF
> 3 - GHI
> 4 - JKL
> 1 - ABC
>
> Any ideas on why the sort is not working? Is there something else I am
> missing? I've applied sorts to all kinds of dataviews before and never had > this happen, and I've spent 6 hours trying to figure out why it will
> not
> work. I've even tried sorting on column "B", and tried sorting DESC on each > column too. Nothing seems to work.
>
> Any help is greatly appreciated.
>
> --
>
> Raymond Lewallen
>
>
>


Nov 21 '05 #9
> Why do you need to sort table?

I don't now that I know there is a rows collection of sorts for the
dataview, i.e. the DataRowView. Thats not something that was easy to find
associated with a dataview when looking up dataview and sorting in the help
files.
Why don't you just use DataView?


I am now that I have your solution along with Jim's and Cor's.

Thank you for posting a solution,

--

Raymond Lewallen
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
9
by: Raymond Lewallen | last post by:
I have a dataview in which the sort property will not sort the dataview. Here's is a simple scenario similar to what I am doing: Class Foo Private Function Retrieve() As DataView ' Returns a...
5
by: David Wender | last post by:
I want to create a dataview with a sort on multiple columns. However, when I use FindRows, I only want to search some of the columns, not all. Is this possible? I have not been able to make it...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.