|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to resort records in a dataSet?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 } The DataView is the correct path. Let's see your code.
-- Show quote____________________________________ 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" <And***@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 > } > > > 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. -- Show quote____________________________________ 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" <And***@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 >> } >> >> >> > Andrew,
The dataview does not sort the table, it is a filter that shows the datarows using a datarowview in a sorted order. Cor 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)? Show quote "Cor Ligthert[MVP]" wrote: > Andrew, > > The dataview does not sort the table, it is a filter that shows the datarows > using a datarowview in a sorted order. > > Cor > 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 Show quote "Andrew" <And***@discussions.microsoft.com> wrote in message news:06DD409D-3E93-4300-A874-85B465668CE1@microsoft.com... > 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)? > > "Cor Ligthert[MVP]" wrote: > >> Andrew, >> >> The dataview does not sort the table, it is a filter that shows the >> datarows >> using a datarowview in a sorted order. >> >> Cor >> You can in Visual Studio versions after 2003 use the overloaded
dataview.totable. Be aware that it creates a new datatable. http://msdn2.microsoft.com/en-us/library/system.data.dataview.totable.aspx Cor |
|||||||||||||||||||||||