IT Community - Software Programming, Web Development and Technical Support

How can we sort the values in dataset?

This is a discussion on How can we sort the values in dataset? within the C# Programming forums, part of the Software Development category; Can any body give me solution for this? How can we sort the values in dataset?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-26-2008, 03:38 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Question How can we sort the values in dataset?

Can any body give me solution for this?

How can we sort the values in dataset?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 04:45 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: How can we sort the values in dataset?

Yeap buddy....

You can sort by using dataview. See the below,

Code:
SqlConnection sql=new SqlConnection(ConnectionString);
			SqlCommand cmd=new SqlCommand("Sp_Test",sql);
			cmd.CommandType=CommandType.StoredProcedure;			
			SqlDataAdapter da=new SqlDataAdapter(cmd);
			DataSet ds=new DataSet();
			da.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataTable dt=new DataTable();
				dt.Columns.Add("Score",typeof(double));
				dt.Columns.Add("Name",typeof(string));
				DataRow dr;
				foreach(DataRow oUserRoleRow in ds.Tables[0].Rows)
				{
					dr = dt.NewRow();
					dr["Score"] = oUserRoleRow["intScore"];
					dr["Name"]=oUserRoleRow["strGameName"];
					dt.Rows.Add(dr);
				}
				DataView dv=dt.DefaultView;
				dv.Sort="Score desc";
				DataGrid drg=new DataGrid();
				drg.DataSource=dv;
				drg.DataBind();
				this.Controls.Add(drg);
			}
That's it...
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 05:10 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: How can we sort the values in dataset?

How about if we want to filter or sort the data further once we've retrieved it from the database? The simplest way (if we're not looking to DataBind to a control), is to use the Select method of the DataTable, which accepts two parameters - a filter expression and a sort expression - and returns an array of DataRow objects.

Code:
DataRow[] matchingRows = dataSet.Tables["users"].Select("username like 'Bob%'","dateJoined DESC");
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-26-2008, 05:11 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: How can we sort the values in dataset?

Another method is to use the DataView object, which is specifically designed for sorting and filtering rows, and can be used as a DataSource in its own right - so we could bind a DataGrid control to this customised "view". We can get an instance of a DataView object from the DefaultView property of our DataTable. Then, we can sets its Sort and RowFilter properties:

Code:
DataView dataView = dataSet.Tables["users"].DefaultView;
dataView.RowFilter = "username like 'Bob%'"; 
dataView.Sort = "dateJoined DESC";

myDataGrid.DataSource = dataView;
//Call to DataBind needed in ASP.NET
//myDataGrid.DataBind();
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 03:39 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How can we sort the values in dataset?

private static void SortDataTable(DataTable dt, string sort)
{
DataTable newDT = dt.Clone();
int rowCount = dt.Rows.Count;

DataRow[] foundRows = dt.Select(null, sort); // Sort with Column name
for (int i = 0 ; i < rowCount; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=foundRows[i][j];
}
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);
}

//clear the incoming dt
dt.Rows.Clear();

for(int i = 0; i < newDT.Rows.Count; i++)
{
object[] arr = new object[dt.Columns.Count];
for (int j = 0; j < dt.Columns.Count; j++)
{
arr[j]=newDT.Rows[i][j];
}

DataRow data_row = dt.NewRow();
data_row.ItemArray = arr;
dt.Rows.Add(data_row);
}

}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort Datatable oxygen C# Programming 2 04-22-2008 09:25 PM
How to sort object array? raja C# Programming 4 03-14-2008 08:52 PM
Read XML by Dataset gp_logesh ASP and ASP.NET Programming 0 10-31-2007 02:13 AM
Dif..sort();asort();ksort() Arun Flash Actionscript Programming 1 09-07-2007 07:14 AM
Can anybody explain about the differences between dataset.clone and dataset.copy? kingmaker C# Programming 2 08-27-2007 11:22 PM


All times are GMT -7. The time now is 09:34 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0