I'm working on some old code (not mine) which, unfortunately, has a lot of hard-coded code in it which has been causing issues that I'm now trying to fix. Here's my issue right now:
There is a gridview with 14 columns representing two weeks. The first day is either a Monday or Sunday depending on a boolean in the code which checks which "type" a user is.
Now, all days are layed out like so (this is for Sunday):
<asp:TemplateField HeaderText="SUN">
<footertemplate>
<asp:Label ID="lblD1F" runat="server" ForeColor="white" Width="35px" Text="<%# GetTotal(0).ToString() %>" />
</footertemplate>
<headertemplate>
<asp:Label ID="lblD1H" runat="server" CssClass="hdr_Day" Text="SUN"></asp:Label><br />
<asp:Label ID="lblD1D" runat="server" CssClass="hdr_Date" Text='<%# _displayDate.ToString("MM/dd") %>'></asp:Label>
</headertemplate>
<itemtemplate>
<anthem:TextBox id="tbDay1"
runat="server"
Text='<%# Bind("Day1") %>'
CssClass="tbWeekEnd"
AutoCallBack="true" />
<asp:Label ID="lblDay1" runat="server" Visible="false" Text='<%# Bind("Day1") %>'></asp:Label>
</itemtemplate>
<itemstyle cssclass="cell_weekend" />
</asp:TemplateField>
So 14 days set-up like above result in below:
Now, I was trying to make it so it would start on Sunday OR Monday, based on the user. I pretty much hard-coded new strings over top the old ones, which is just starting to cause more problems. First I made two constant strings:
String[] userADays = new String[] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
String[] userBDays = new String[] { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
protected void gvMasterProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This if/else statement overrides the hard coded date headers in the timeEntry aspx files since the days were hardcoded in.
//Populates the cells w/ the data from one of two arrays depending if the user is A or B.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.CssClass = "HeaderStyle";
if (isUserB == false)
{
for (int i = 9; i < 23; i++)
{
e.Row.Cells[i].Font.Size = 10;
e.Row.Cells[i].Text = userADays[i - 9] + "\n" + " " + _displayDate.AddDays(i-9).ToString("MM/dd") + " ";
}
}
else
{
for (int i = 9; i < 23; i++)
{
e.Row.Cells[i].Font.Size = 10;
e.Row.Cells[i].Text = UserBDays[i - 9] + "\n" + " " + _displayDate.AddDays(i - 9).ToString("MM/dd") + " ";
}
}
}
Here's the gridview stuff in the .aspx file:
<anthem:GridView
ID="gvMasterProjects"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
OnRowDataBound="gvMasterProjects_RowDataBound"
AllowPaging="false"
EnableViewState="true"
HorizontalAlign="Center"
AlternatingRowStyle-CssClass="AlternatingRowStyle"
RowStyle-CssClass="RowStyle"
HeaderStyle-CssClass="HeaderStyle" style="margin-left: 71px">
I really want to do away with this hard-coding. There's got to be an easier way to have these dates loaded w/o doing what's being done right now. Any help would be greatly appreciated.
Edit: Attempted the answers below, which work except that there are many "UpdateAfterCallBack" lines that are executed which result in the dates being reverted to the hardcoded ones in .aspx. Is there an easy way to do this in .aspx?