在Gridview标题中显示一周中的7天

时间:2015-11-24 16:50:04

标签: c# asp.net gridview

我试图在GridView标题内显示一周中的7天,因此下面的代码可以工作,但是希望根据下拉选项显示一周中的7天。我已经下载了:

WeekStart      WeekEnd
2015-11-15    2015-11-21
2015-11-22    2015-11-28

等等..所以如果选择下拉列表是2015-11-15那么我的gridview标题应该从2015-11-15到2015-11-21开始显示。再次,如果下拉列表中的selectoin是2015-11-22,那么gridview标题应该从所选日期开始显示:2015-11-22等等..我的代码目前开始显示今天的日期但是我想开始根据下拉选择显示标题

 <asp:DropDownList ID="ddlSelectWeek" runat="server" OnSelectedIndexChanged="ddlSelectWeek_SelectedIndexChanged" Width="200px">
                <asp:ListItem Text="2015-11-15" Value="2015-11-15"></asp:ListItem>
                <asp:ListItem Text="2015-11-22" Value="015-11-22"></asp:ListItem>
                <asp:ListItem Text="2015-11-29" Value="2015-11-29"></asp:ListItem>
            </asp:DropDownList>


 protected void ddlSelectWeek_SelectedIndexChanged(object sender, EventArgs e)
    {
      BindGridview();
    }


 protected void BindGridview()
    {
      if (!this.IsPostBack)
      {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[8] {
       new DataColumn("Category"),
       new DataColumn("Day1"),
       new DataColumn("Day2"),
       new DataColumn("Day3"),
       new DataColumn("Day4"),
       new DataColumn("Day5"),
       new DataColumn("Day6"),
       new DataColumn("Day7"),
   });
        //add your category
        dt.Rows.Add("Travelling");
        dt.Rows.Add("Food");
        dt.Rows.Add("Air Fare,");
        dt.Rows.Add("Auto Rental");

        gvCategory.DataSource = dt;
        gvCategory.DataBind();
        //for generating datewise coloumn
        for (int i = 1; i < gvCategory.HeaderRow.Cells.Count; i++)
        {
          gvCategory.HeaderRow.Cells[i].Text = DateTime.Now.AddDays(i - 1).ToString("ddd") + " " + DateTime.Now.AddDays(i - 1).ToShortDateString();
        }
      }
    }

1 个答案:

答案 0 :(得分:1)

试一试。

                                                                               

protected void ddlSelectWeek_SelectedIndexChanged(object sender, EventArgs e)
{
    DateTime theDate;
    DateTime.TryParseExact(this.ddlSelectWeek.SelectedValue, "yyyy-MM-dd", CultureInfo.InvarientCulture, DateTimeStyles.None, out theDate);
    BindGridview(theDate);
}

protected void BindGridview(DateTime theDate)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[8] {
    new DataColumn("Category"),
    new DataColumn("Day1"),
    new DataColumn("Day2"),
    new DataColumn("Day3"),
    new DataColumn("Day4"),
    new DataColumn("Day5"),
    new DataColumn("Day6"),
    new DataColumn("Day7")
    });

    //add your category
    dt.Rows.Add("Travelling");
    dt.Rows.Add("Food");
    dt.Rows.Add("Air Fare,");
    dt.Rows.Add("Auto Rental");

    gvCategory.DataSource = dt;
    gvCategory.DataBind();
    //for generating datewise coloumn
    for (int i = 1; i < gvCategory.HeaderRow.Cells.Count; i++)
    {
        gvCategory.HeaderRow.Cells[i].Text = theDate.AddDays(i - 1).ToString("ddd") + " " + theDate.AddDays(i - 1).ToShortDateString();
    }
}