Hi elvisidrizi1,
You need to use OrderBy with ThenBy for specifying the ordering like did in the SQL Case statement.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView ID="gvDetails" runat="server">
</asp:GridView>
NameSpace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Price"),
new DataColumn("Day",typeof(string)),
new DataColumn("UserName")
});
dt.Rows.Add("31.560285", "Fri", "Elvis");
dt.Rows.Add("39.854615", "Thu", "Elvis");
dt.Rows.Add("33.112272", "Sun", "Elvis");
dt.Rows.Add("44.222549", "Tue", "Elvis");
dt.Rows.Add("37.551000", "Sat", "Elvis");
dt.Rows.Add("10.000000", "Fri", "test-1");
dt.Rows.Add("43.727317", "Wed", "Elvis");
dt.Rows.Add("38.036363", "Mon", "Elvis");
List<string> result = (from p in dt.AsEnumerable()
select p.Field<string>("Day")).Distinct()
.OrderBy(x => x == "Mon" ? 0 : 7)
.ThenBy(x => x == "Tue" ? 1 : 7)
.ThenBy(x => x == "Wed" ? 2 : 7)
.ThenBy(x => x == "Thu" ? 3 : 7)
.ThenBy(x => x == "Fri" ? 4 : 7)
.ThenBy(x => x == "Sat" ? 5 : 7)
.ThenBy(x => x == "Sun" ? 6 : 7)
.ToList();
gvDetails.DataSource = result;
gvDetails.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Price"),
New DataColumn("Day", GetType(String)),
New DataColumn("UserName")})
dt.Rows.Add("31.560285", "Fri", "Elvis")
dt.Rows.Add("39.854615", "Thu", "Elvis")
dt.Rows.Add("33.112272", "Sun", "Elvis")
dt.Rows.Add("44.222549", "Tue", "Elvis")
dt.Rows.Add("37.551000", "Sat", "Elvis")
dt.Rows.Add("10.000000", "Fri", "test-1")
dt.Rows.Add("43.727317", "Wed", "Elvis")
dt.Rows.Add("38.036363", "Mon", "Elvis")
Dim result As List(Of String) = (From p In dt.AsEnumerable()
Select p.Field(Of String)("Day")).Distinct() _
.OrderBy(Function(x) If(x = "Mon", 0, 7)) _
.ThenBy(Function(x) If(x = "Tue", 1, 7)) _
.ThenBy(Function(x) If(x = "Wed", 2, 7)) _
.ThenBy(Function(x) If(x = "Thu", 3, 7)) _
.ThenBy(Function(x) If(x = "Fri", 4, 7)) _
.ThenBy(Function(x) If(x = "Sat", 5, 7)) _
.ThenBy(Function(x) If(x = "Sun", 6, 7)).ToList()
gvDetails.DataSource = result
gvDetails.DataBind()
End If
End Sub
Screenshot