Question

<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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top