Re: How to resort records in a dataSet? by William
William
Mon Nov 05 10:41:18 PST 2007
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" <billvaNoSPAM@betav.com> wrote in message
news:uTEa1ikHIHA.1188@TK2MSFTNGP04.phx.gbl...
> 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" <Andrew@discussions.microsoft.com> wrote in message
> news:A315398B-FE8E-41C0-943B-01608EA9F883@microsoft.com...
>> 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
>> }
>>
>>
>>
>