Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I tried to sort data in DataSet by this code but it failed, why ?

topicsEditDataSet.Tables[0].DefaultView.Sort = "TopicID DESC";


any body has any idea please ?
Posted

You shouldn't use the DefaultView property instead you should create a new DataView. The reason for creating a new is;
it doesn't work when setting the property to "DefaultView" is because you have to access the DefaultView, not the DataSet to get the sorted results. Manipulating the DefaultView is usually not a good idea.

Basically, create a new instance of the DataView class and pass that around after you set the sort, and work with that.
//open the connection
sqlConnection.Open();

//create a new sqladapter and set its command object with our sqlcommand
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
//create a new dataset to hold our data
DataSet ds = new DataSet();
//fill the dataset with the result of our query from the specified command
sqlAdapter.Fill(ds);

//create a new dataview from our table in our dataset at index 0
DataView dv = new DataView(ds.Tables[0]);

//apply our row filter. get only records with a lastname of 'Callahan'
dv.RowFilter = "Name = 'Brown'";

//close our connection
sqlConnection.Close();

//return our dataview
return dv;



for a reference see this link;

Sort datatable[^]
 
Share this answer
 
v2
Comments
MrLonely_2 8-Mar-11 23:23pm    
Thanks....... it worked correctly
Orcun Iyigun 8-Mar-11 23:27pm    
you are welcome.
Link1[^] and Link2[^] might help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900