Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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()%>'>
                 </asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Wednesday">
             <ItemTemplate>
                 <asp:Label ID="lblWednesday" runat="server" Text='<%# Eval("tsWednesday").ToString()%>'>
                 </asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Thursday">
             <ItemTemplate>
                 <asp:Label ID="lblThursday" runat="server" Text='<%# Eval("tsThursday").ToString()%>'>
                 </asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Friday">
             <ItemTemplate>
                 <asp:Label ID="lblFriday" runat="server" Text='<%# Eval("tsFriday").ToString()%>'>
                 </asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="Saturday">
             <ItemTemplate>
                 <asp:Label ID="lblSaturday" runat="server" Text='<%# Eval("tsSaturday").ToString()%>'>
                 </asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
            <%-- <asp:BoundField DataField="tsMonday" HeaderText="lblMonday" />
             <asp:BoundField DataField="tsTuesday" HeaderText="lblTuesday" />
             <asp:BoundField DataField="tsWednesday" HeaderText="lblWednesday" />
             <asp:BoundField DataField="tsThursday" HeaderText="lblThursday" />
             <asp:BoundField DataField="tsFriday" HeaderText="lblFriday" />
             <asp:BoundField DataField="tsSaturday" HeaderText="tsSaturday" />--%>
 

             <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
             <asp:TemplateField>
                 <itemtemplate>
                 <footertemplate>
 
             <asp:Button ID="btnADD" runat="server" Text="Add New" OnClick="btnADD_Click" />
 
         </footertemplate>
             </itemtemplate>
             </asp:TemplateField>
     </Columns>
     <RowStyle BackColor="#E3EAEB" />
     <EditRowStyle BackColor="#7C6F57" />
     <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
     <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
     <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
     <AlternatingRowStyle BackColor="White" />
 </asp:GridView>
 

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
Edited 9-Dec-12 18:39pm
v2
Comments
pradiprenushe at 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 at 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 at 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 at 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
0 Maciej Los 404
1 OriginalGriff 369
2 Sergey Alexandrovich Kryukov 158
3 CPallini 145
4 Hard_Rockz 85
0 OriginalGriff 5,634
1 Sergey Alexandrovich Kryukov 4,763
2 Peter Leow 2,954
3 Maciej Los 2,614
4 DamithSL 2,465


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