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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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);
} ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 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! |
| |||
| 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! |
| |||
| 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); } } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |