0

Hi I have database where i am saving the weekdays as numbers (such as Sunday as 1 Monday as 2 and so on). I am binding this info to a grid view. What i want to do is when displaying weekdays in the grid view i want instead of 1234567 it should appear as "SMTWThFSa". I am trying Case query but then i have to make several case queries such as

select (case when Weekdy='12' then 'SM' end) as Weekdy from Wc_Batches where BatchID='B03141'

can someone help me with this please. Thanks.

CrazySwazy
  • 403
  • 1
  • 5
  • 11

1 Answers1

0

I think it should be done in code level, not SQL. Change SQL query to:

select Weekdy from Wc_Batches

In ascx/aspx add GridView:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Days" HeaderText="Week days" />
    </Columns>
</asp:GridView>

Then in code behind write:

var weekdays = GetWeekdays(); // some method that executes sql query and returns list.
var dataList = weekdays.Select(d => new
    {
        Days = string.Join(string.Empty, d.Select(c => GetWeekDayFromInt(int.Parse(c.ToString())))))
    });

gv.DataSource = dataList;
gv.DataBind();

...

public string GetWeekDayFromInt(int day)
{
    switch (day)
    {
        case 1:
            return "S";
        case 2:
            return "M";
        case 3:
            return "T";
        ... // other days
    }
    return string.Empty;
}
Ivan Doroshenko
  • 944
  • 7
  • 13