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

Dataview will not sort

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


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

Jul 21 '05 #2

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

Jul 21 '05 #3

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

Jul 21 '05 #4

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


Jul 21 '05 #5

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

Jul 21 '05 #6

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



Jul 21 '05 #7

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


Jul 21 '05 #8

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


Jul 21 '05 #9

P: n/a
> 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
Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.