i have a table with a column named size which will have values big,medium and开发者_运维问答 small. wat i want is i want to sort this column in such a way that all rows with size big should come first, then rows with size medium and finally rows with size small. is there a way by which i can achieve this?
edit: it is a data table which i add to a dataset.
edit: wat if the initials of the words, in this case b,m and s, are not in alphabetical order. in that wat am i supposed to do coz i have another application as well where i'll be required to sort a column having value High, normal and low.
Example
DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction; //sortexpression will be fieldname direction will be ascending descending
GridView1.DataSource = dv;
GridView1.DataBind()
Have a look at this article: Express Yourself with Expression-based Columns
http://msdn.microsoft.com/en-us/library/ms810291.aspx
OF COURSE IT WORKS!
DataSet myDs = new DataSet();
DataTable myDt = new DataTable("Table1");
myDs.Tables.Add(myDt);
myDt.Columns.Add("Size", typeof(string));
myDt.Columns.Add("Ranking", typeof(int), "Iif((Size)='Large', 1, Iif((Size)='Medium', 2, Iif((Size)='Small', 3, 4)))");
DataRow myDr1 = myDs.Tables["Table1"].NewRow();
DataRow myDr2 = myDs.Tables["Table1"].NewRow();
DataRow myDr3 = myDs.Tables["Table1"].NewRow();
DataRow myDr4 = myDs.Tables["Table1"].NewRow();
myDr1["Size"] = "Large";
myDr2["Size"] = "Medium";
myDr3["Size"] = "Small";
myDr4["Size"] = "Large";
myDt.Rows.Add(myDr1);
myDt.Rows.Add(myDr2);
myDt.Rows.Add(myDr3);
myDt.Rows.Add(myDr4);
DataView myDv = new DataView(myDt);
myDv.Sort = "Ranking";
ultraGrid1.DataSource = myDv;
And that is the code that proves it.
Map big, medium, small to an enum in the correct order and sort on that. Any other answer will require more information as asked for in the comments.
If this is really as masqueraded T-SQL question, you could either use UNION
(as proposed by Lukasz), or you could use a few WHEN ... THEN
constructs in your ORDER BY
:
SELECT *
FROM SomeTable
ORDER BY WHEN size = 'big' THEN 2 WHEN 'medium' THEN 1 ELSE 0 END
This might also work in ADO.NET, but I have not tested it:
myDataTable.DefaultView.Sort =
"WHEN size = 'big' THEN 2 WHEN 'medium' THEN 1 ELSE 0 END";
EDIT: David points out, that in your specific case (big, medium, small), the alphabetical sort order matches the expected ordering. In this special case, you can simply do:
ORDER BY size
Or in ADO.NET:
myDataTable.DefaultView.Sort = "size";
If you want a simple solution (and if these values are unlikely to change), just order by the 'size' column, since the big, medium and small words are in alphabetical order :D
Using the built-in sorting algorithm of the DataView
(which is where you'd do actual sorting, not in the DataTable
itself), this isn't possible, strictly speaking; there is no support for "custom" sorting, which is what you'd need. The sorting capabilities of the DataView
are actually surprisingly limited, and (IIRC) it isn't even a stable sort (since it uses Array.Sort
, which is documented as unstable).
Your only real option (from a GUI perspective) is to use a data display control that provides custom sorting capability, like the DevExpress XtraGrid.
After edits by OP, it has become (somewhat) clear that we are dealing with a DataTable
.
One approach to sorting the rows of a DataTable
according to a "mapped value", would be to use its Select
method to retrieve several arrays (one for each value) of rows:
List<DataRow> rows = new List<DataRow>();
rows.AddRange(oldTable.Select("size = 'Big'"));
rows.AddRange(oldTable.Select("size = 'Medium'"));
rows.AddRange(oldTable.Select("size = 'Small'"));
If needed, the rows can be imported into a fresh DataTable
with the same schema:
DataTable newTable = oldTable.Clone();
foreach (DataRow row in rows)
{
newTable.ImportRow(row);
}
This approach is definitely not very efficient, but it's probably about the easiest way to do this.
Try a switch statement.
Switch(var)
{ Case "Big":
//Append to TextBox1
//Or add to var1[]
break;
Case "Small":
//Append to TextBox2
//Or add to var2[]
break;
Case "Medium":
//Append to TextBox3
//Or add to var3[]
break;
}
EDIT: Sort into txtbox's or var[]'s, does this go-round make more sense?
精彩评论