Click here to Skip to main content
12,455,701 members (79,512 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0 Gridview
hi
here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
     DataKeyNames="tsID" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"
     OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
     Height="168px" OnRowDataBound="GridView1_RowDataBound1">
     <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
     <Columns>
         <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
         <asp:BoundField DataField="tsID" HeaderText="ID" ReadOnly="true" />
         <asp:BoundField DataField="tsProject" HeaderText="Project" />
         <asp:BoundField DataField="tsStartDate" HeaderText="Start Date" />
         <asp:BoundField DataField="tsEndDate" HeaderText="End Date" FooterText="Total" />
         <asp:TemplateField HeaderText="Monday">
             <ItemTemplate>
                 <asp:Label ID="lblMonday" runat="server" Text='<%# Eval("tsMonday").ToString()%>'>
                 </asp:Label>
 
             </ItemTemplate>
             <FooterTemplate>
  <asp:Label ID="lblTotal" runat="server"></asp:Label>
  </FooterTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Tuesday">
             <ItemTemplate>
                 <asp:Label ID="lblTuesday" runat="server" Text='<%# Eval("tsTuesday").ToString()%>'>


so for lblMonday...to lblSaturday want to display week days how can i do tat..

here is some code that i tried but error is lblMonday....lblSaturday not exist..

or i can use rowcreated event??it will work??
please suggest me??


DateTime dt = DateTime.Now;
       //int i = (int)dt.DayOfWeek;
       //
       //lblSunDate.Text = (dt.AddDays(7 - (int)dt.DayOfWeek)).ToShortDateString();
       lblSatuday.Text = (dt.AddDays(6 - (int)dt.DayOfWeek)).ToShortDateString();
       lblFriday.Text = (dt.AddDays(5 - (int)dt.DayOfWeek)).ToShortDateString();
       lblThursday.Text = (dt.AddDays(4 - (int)dt.DayOfWeek)).ToShortDateString();
       lblWednesday.Text = (dt.AddDays(3 - (int)dt.DayOfWeek)).ToShortDateString();
       lblTuesday.Text = (dt.AddDays(2 - (int)dt.DayOfWeek)).ToShortDateString();
       lblMonday.Text = (dt.AddDays(1 - (int)dt.DayOfWeek)).ToShortDateString();

Please Suggest me what is the issue?
or how can i set in grid.i guess its different to set for grid label??

thank you..
Posted 9-Dec-12 18:04pm
ythisbug2.1K
Updated 9-Dec-12 18:39pm
v2
Comments
pradiprenushe 10-Dec-12 1:59am
   
Sorry but dont understand actual requirement beacause in title you said that you want to set header & here you are trying to set data. Can u please elaborate more.
ythisbug 10-Dec-12 5:00am
   
Porblem is in header i want to set date in grid..like i want to add 6 days from monday to satturday..tat date i want to disply it in grid header..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

DateTime startDate = new DateTime(2012, 1, 1);
DateTime endDate = new DateTime(2012, 12, 12);
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
{
    DayOfWeek dw = date.DayOfWeek;
    // write your code..
}
This will loop through all days from the start date to the end date and get the day of week for each
  Permalink  
Comments
ythisbug 10-Dec-12 0:20am
   
thank you.but am not able to use lblMonday in code..so m not sure that wethr we can use grid header like label to bind days of week..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If you want to set header
1. use this after
GridView1.DataBind();
GridView1.Columns[3].HeaderText = Datetime.Now().Tostring(); // Use your logic here

2. in rowdatbound refer this link
http://www.devcurry.com/2011/03/change-aspnet-gridview-header-text-at.html

protected void GridVie1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowType == DataControlRowType.HeaderRow)
    {
GridView1.Columns[2].HeaderText = Datetime.Now().ToShortStrong(); 
// use your logic here
// I have just given the way you can assign header
GridView1.Columns[2].HeaderText = Datetime.Now().AddDays(1).ToShortStrong();
}
}
  Permalink  
v2
Comments
ythisbug 10-Dec-12 6:16am
   
i tried ur code.but no output only..

i tried this code bt am getting 3 rows
here is my code..

in gridview row created event

headerCell = new TableCell();
headerCell.Text = (dt.AddDays(1 - (int)dt.DayOfWeek)).ToShortDateString();
// headerCell.Text = dt.AddDays(0).DayOfWeek.ToString();
HeaderGridRow.Cells.Add(headerCell);


headerCell = new TableCell();
headerCell.Text = (dt.AddDays(2 - (int)dt.DayOfWeek)).ToShortDateString();
HeaderGridRow.Cells.Add(headerCell);


headerCell = new TableCell();
headerCell.Text = (dt.AddDays(3 - (int)dt.DayOfWeek)).ToShortDateString();
HeaderGridRow.Cells.Add(headerCell);


headerCell = new TableCell();
headerCell.Text = (dt.AddDays(4 - (int)dt.DayOfWeek)).ToShortDateString();
HeaderGridRow.Cells.Add(headerCell);



headerCell = new TableCell();
headerCell.Text = (dt.AddDays(5 - (int)dt.DayOfWeek)).ToShortDateString();
HeaderGridRow.Cells.Add(headerCell);


headerCell = new TableCell();
headerCell.Text = (dt.AddDays(6 - (int)dt.DayOfWeek)).ToShortDateString();
HeaderGridRow.Cells.Add(headerCell);


GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow);
can any one suggest me wat is the error..
am getting 3 rows ..i mean its adding 3 rows for same date in header
like
12/10/2012...........12/15/2012
12/10/2012...........12/15/2012
12/10/2012...........12/15/2012
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
   {
 
       DateTime dt = DateTime.Now;
       if (e.Row.RowType == DataControlRowType.Header)
       {
           GridView1.Columns[5].HeaderText = dt.AddDays(0).ToShortDateString();
           GridView1.Columns[6].HeaderText = dt.AddDays(1).ToShortDateString();
           GridView1.Columns[7].HeaderText = dt.AddDays(2).ToShortDateString();
           GridView1.Columns[8].HeaderText = dt.AddDays(3).ToShortDateString();
           GridView1.Columns[9].HeaderText = dt.AddDays(4).ToShortDateString();
           GridView1.Columns[10].HeaderText = dt.AddDays(5).ToShortDateString();
       }

}

i hv solved like this..thank you
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100