How to resort records in a dataSet?

G

Guest

Hello, friends,

We have an app in c#.net 2003. There is a dataSet. Its data are populated to
a customized list in a loop (NO data binding, now and future). We need to
modify it so that it can be sorted based on user's selections.

I tried the follows by using DataView, but no luck: It still keeps the
original order.

Any ideas? Any other methods that we can resort records in a dataSet? Thanks !

-----------------------

DataView mDataView = new DataView();
mDataView = mDataSet.Tables[0].DefaultView;
mDataView.Sort = "--user's selections--";

mDataSet.Tables.Clear();
mDataSet = null;
DataSet mDataSet2 = new DataSet();
mDataSet2.Tables.Add(mDataView.Table);

foreach (DataRow dr in mDataSet2.Tables[0].Rows)
{
//check the order
}
 
W

William Vaughn

The DataView is the correct path. Let's see your code.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
C

Cor Ligthert[MVP]

Andrew,

The dataview does not sort the table, it is a filter that shows the datarows
using a datarowview in a sorted order.

Cor
 
G

Guest

Thanks. Then, is there a way to resort records of a data set? Or, in ADO.net,
one has no way to resort records, (unless to query database with a new Order
By statement)?
 
T

TAJ

You would need to re-query or use the view to sort. One way is to serialize
the data to xml, sort, then deserialize. Lots of work when you have a
dataview.

TAJ
 
W

William \(Bill\) Vaughn

Huh?
cmd = New SqlCommand("SELECT Author, Year_Born FROM Authors ",
cn)
cn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
Dim dv As New DataView
dv = dt.DefaultView
dv.Sort = "Year_Born Desc"
DataGridView1.DataSource = dv
cn.Close()

Isn't this what you want?
This does not physically resort the table but why would you want to? This
provides a sorted "view" of the table that can be resorted on any column or
set of columns in either ascending (the default) or descending order.



--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
William Vaughn said:
The DataView is the correct path. Let's see your code.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

Andrew said:
Hello, friends,

We have an app in c#.net 2003. There is a dataSet. Its data are populated
to
a customized list in a loop (NO data binding, now and future). We need to
modify it so that it can be sorted based on user's selections.

I tried the follows by using DataView, but no luck: It still keeps the
original order.

Any ideas? Any other methods that we can resort records in a dataSet?
Thanks !

-----------------------

DataView mDataView = new DataView();
mDataView = mDataSet.Tables[0].DefaultView;
mDataView.Sort = "--user's selections--";

mDataSet.Tables.Clear();
mDataSet = null;
DataSet mDataSet2 = new DataSet();
mDataSet2.Tables.Add(mDataView.Table);

foreach (DataRow dr in mDataSet2.Tables[0].Rows)
{
//check the order
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top