0
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False">

   <Columns>
      <asp:BoundField DataField='id' HeaderText="Id" Visible="False" />
      <asp:BoundField DataField='Name' Visible="True"  HeaderText="Full Name"/>


     <asp:BoundField  HeaderText="Monday"  />   
     <asp:BoundField  HeaderText="Tuesday" />   
     <asp:BoundField  HeaderText="Wednesday"  />   
     <asp:BoundField  HeaderText="Thursday"  />   
      <asp:BoundField HeaderText="Friday"  />              
  </Columns>

</asp:GridView>

<asp:TextBox ID="tbFirstDate" runat="server"></asp:TextBox>

//In the DataTable that is the source for this grid1 I have a column 'date', but in the Grid I need to show weekday with that date (ex. Monday 20.08.2012). I also have a TextBox(tbFirstDate) that defines which day is monday, so using that info I need to calculate the rest of the weekdays, date format is yyyy-mm-dd

I work with PostreSQL, this is the SQL query:

SELECT  *  FROM Table1
WHERE  id1=212 AND date BETWEEN '20.08.2012' AND  '24.08.2012'
ORDER BY date

enter image description here

Now this result I want to represent in the grid like this

enter image description here

Alex
  • 8,908
  • 28
  • 103
  • 157
  • 1
    Can you please illustrate how exactly your grid should look like? – Amiram Korach Aug 28 '12 at 15:17
  • So basically your actual question is how to change a `DateTime's` `DayOfWeek` value by a specified number of days? – Tim Schmelter Aug 28 '12 at 15:44
  • @AmiramKorach I added a picture of my grid, maybe now it's more clear – Alex Aug 29 '12 at 06:59
  • It's still not clear. Is this just a SQL question, do you know how to group by? What does _"define which day is monday"_ actually mean? Can the user select a friday instead of a monday to define the offset of a given day? What is _value_, is it a string,char,number or date that needs to be grouped also or just an indicator for something? – Tim Schmelter Aug 29 '12 at 07:13
  • @TimSchmelter The TextBox is designed to receive the value of monday only, and the main question is how to calculate the day of the week for each the dates i have using the monday offset – Alex Aug 29 '12 at 07:51

1 Answers1

2

So basically your actual question is how to change a DateTime's DayOfWeek value by it's distance to a specified first day of week?

Then you could use this method:

public static int Weekday(DateTime dt, DayOfWeek startOfWeek)
{
    return (dt.DayOfWeek - startOfWeek + 7) % 7;
}

For example, assuming that the user entered today to specify that Tuesday is the beginning of the week:

DateTime value = new DateTime(2012, 8, 20);
DayOfWeek weekday = (DayOfWeek)Weekday(value, DateTime.Now.DayOfWeek);
Console.Write("normally:{0} changed to:{1}", value.DayOfWeek, weekday); 

Demo: http://ideone.com/Zk99b

If this is true you only have to use this method in RowDataBound of the GridView to set the Text of a Label in a TemplateField manually according to the specified date in tbFirstDate and the date value in the DataItem of the GridViewRow. Use GridViewRow.FindControl("LabelID") to get a reference to the Label.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939