Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - Sort rows of DataTable in particular order we want

Consider I have DataTable with two columns

Column1 Column2
abc jan
def dec
cba feb
bdf nov
aaa dec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add an expression based column

dataExistingTable.Columns.Add("MonthNumber", typeof(int),
    "IIF ([Column2] = 'dec', 12, (IIF ([Column2] = 'nov', 11, (IIF ([Column2] = 'oct', 10, (IIF ([Column2] = 'sep', 9, 8)))))))");

Then apply sort on expression based column

dataExistingTable.DefaultView.Sort = "MonthNumber desc";

Then DefaultView of the table will give your sorted rows.

// dataExistingTable.DefaultView;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...