将GridView列拆分为两列,因为我的附加图像显示在ASP.NET中

时间:2016-11-11 16:15:22

标签: asp.net gridview

我想将每个单元格分成两列,并在这些分割的单元格中加入一些值。我创建了我的标题如下

if (e.Row.RowType == DataControlRowType.Header)
    {


                e.Row.Cells[0].Text = "";
                e.Row.Cells[1].Text = "Monday <br> morning | afternoon";
                e.Row.Cells[2].Text = "Tuesday <br> morning | afternoon";
                e.Row.Cells[3].Text = "Wednsday <br> morning | afternoon";
                e.Row.Cells[4].Text = "Thursday <br> morning | afternoon";
                e.Row.Cells[5].Text = "Friday  <br> morning | afternoon";
                e.Row.Cells[6].Text = "Saturday <br> morning | afternoon";
                e.Row.Cells[7].Text = "Sunday  <br> morning | afternoon";
}

但是我想要它们作为图像nr 2节目。我该如何做到这一点?提前谢谢。enter image description here

更新:这就是我想要实现的目标。

enter image description here

enter image description here

更新:
用户输入startDate和endDate并插入&#34; tplHoliday&#34;我用

private List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
        {
            List<DateTime> allDates = new List<DateTime>();
            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
                allDates.Add(date);
            return allDates;

        }

并将每个日期插入到tplDates。我觉得这个插入是不必要的,但它让我可以轻松选择我的gridView,代码如下

 SqlCommand cmd5 = new SqlCommand((@"select e.FirstName,
                        count(case when d.Dates = @day1 THEN 1 END),
                        count(case when d.Dates = @day2 THEN 1 END),
                        count(case when d.Dates = @day3 THEN 1 END),
                        count(case when d.Dates = @day4 THEN 1 END),
                        count(case when d.Dates = @day5 THEN 1 END),
                        count(case when d.Dates = @day6 THEN 1 END),
                        count(case when d.Dates = @day7 THEN 1 END)
                        from tplEmployee e

                        left join tplDates d
                        on e.EmployeeId = d.EmployeeId
                        group by e.FirstName"), connection);

                cmd5.Parameters.AddWithValue("@day1", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day2", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(1).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day3", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(2).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day4", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(3).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day5", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(4).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day6", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(5).ToShortDateString()));
                cmd5.Parameters.AddWithValue("@day7", (DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(6).ToShortDateString()));




                    connection.Open();

                    GridView1.DataSource = cmd5.ExecuteReader();
                    GridView1.DataBind();


你看到1 ..和0的原因,但我不知道如何处理这个问题。我真的想看到最新更新的图像。我正在尝试和尝试和不同的代码战斗,但我实现了我的目标。我的梦想是获得最新更新的GridView图像...请告诉我该怎么做..

1 个答案:

答案 0 :(得分:1)

这是一个简单的代码片段,它使用周名称向GridView添加标题行。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //cast the sender as gridview
        GridView gridView = sender as GridView;

        //create a new gridviewrow
        GridViewRow gridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

        //add an empty cell
        TableCell tableCellEmpty = new TableCell();
        tableCellEmpty.Text = "";
        gridViewRow.Cells.Add(tableCellEmpty);

        //create a date that is a monday
        DateTime week = new DateTime(2016, 11, 14);

        //add 7 weekday cells
        for (int i = 0; i < 7; i++)
        {
            TableCell tableCell = new TableCell();
            tableCell.Text = week.AddDays(i).ToString("dddd");
            tableCell.ColumnSpan = 2;
            gridViewRow.Cells.Add(tableCell);
        }

        //add the new row to the gridview
        gridView.Controls[0].Controls.AddAt(0, gridViewRow);
    }
}