c# - How to preserve order in Gridview -


in program (asp.net, c#) using gridview show data. gets data following query.

select * nametable nameid in (4,3,1,22,15,8,9,5,7) 

but problem gridview showing data in ascending sort order of nameid (1,3,4,5,7,8,9,15,22). dont want data sorted, should show way mentioned in query (4,3,1,22,15,8,9,5,7)

here code

private void loadgridview() {     query = "select * nametable nameid in (4,3,1,22,15,8,9,5,7)"          dataset ds = sqlhelper.executedataset(commonsettings.constring, commandtype.text, query);         gridview1.datasource = ds;         gridview1.databind(); } 

that pretty difficult code you've provided. doesn't have gridview, it's how data returned sql. sql won't order results way. you'd have specify explicit case in order by this:

query = "select * nametable nameid in (4,3,1,22,15,8,9,5,7) " +         "order case nameid when 4 0 " +                              "when 3 1 " +                              "when 1 2 " +                              "when 22 3 " +                              "when 15 4 " +                              "when 8 5 " +                              "when 9 6 " +                              "when 5 7 " +                              "when 7 8 end"; 

obviously very cumbersome. suggest try sorting column instead.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -