Jonas Willander
Jonas Willander

Reputation: 528

Split a GridView Column into two columns as my attached image shows in ASP.NET

I want to split every cell into two column and put some value in those divided cells. I created my Headers like below

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";
}

But I want them as image nr 2 shows. How do I do to achive that? Thank you in advance.enter image description here

Update: This is what I want to achieve.

enter image description here

enter image description here

Update:
The user input startDate and endDate and inserts to "tplHoliday" the I use

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;

        }

and make insertion every date to tplDates. I feel like this insertion is un necesserily but it gives me easy selection to my gridView with following code

 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();


Tha's why you see 1 .. and 0 but I don't know how to handle this problematics. I realy want to see as last updated image. I'am trying and trying and fighting with different codes but I'an achive my goal. My dream is to achive as last updated GridView image... please tell how how to do..

Upvotes: 0

Views: 2786

Answers (1)

VDWWD
VDWWD

Reputation: 35564

Here is a simple snippet that adds a header row to the GridView with the week names.

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);
    }
}

Upvotes: 1

Related Questions