honag luc

honag luc

  • NA
  • 123
  • 7.5k

Merge column in Datagridview C#

Mar 30 2023 2:53 AM

This code loads the meeting room with the start and end time and the background for the meeting time is teal. but not merged yet from start time to end time into a single box

private void selectAreaColor()
{

    //get the BookingDetail data from api for compare with value in datagridview: Idroom, starttime,stoptime, datebook and load area color to datagridview
    QueryRQ query = CreateQuery();
    BookingIndexRS result = apiBooking.getBooking(query).Result;
    foreach (BookingDetail r in result.Data)
    {

        string idLocal = r.Id.ToString();

        string idroom = r.RoomId.ToString();

        string roomName = r.RoomName.ToString();

        string start = r.Start.ToString();

        string stop = r.Stop.ToString();

        string content = r.Content.ToString();

        string datestring = r.Date.ToString();
        string format = "yyyy-MM-ddTHH:mm:ssZ";
        DateTime datebook = DateTime.ParseExact(datestring, format, CultureInfo.InvariantCulture);

        // Get the start time and end time inputs and the selected meeting room
        DateTime startTime = DateTime.ParseExact(start, "HH:mm", null);
        DateTime stopTime = DateTime.ParseExact(stop, "HH:mm", null);

        if (datebook.Date == dateNgay.Value.Date)
        {
            // Loop through the rows in datagridview and find the matching row
            foreach (DataGridViewRow row in grdRoom.Rows)
            {
                if (row.Cells["Id"].Value.ToString() == idroom)
                {
                    int rowIndex = row.Index;

                    List<string> excluHeadertext = new List<string>() { "08:00","08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30","12:00","12:30",
                     "13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00"};
                    // Loop through the columns and compare the start and end times with the time slots

                    for (int i = 1; i < grdRoom.Columns.Count; i++)
                    {

                        if (excluHeadertext.Contains(grdRoom.Columns[i].HeaderText))
                        {
                            DateTime columnTime = DateTime.ParseExact(grdRoom.Columns[i].HeaderText, "HH:mm", CultureInfo.InvariantCulture);
                            string _HeaderText = grdRoom.Columns[i].Name.ToString();
                            if (startTime == columnTime)
                            {
                                grdRoom.Rows[rowIndex].Cells[_HeaderText].Style.BackColor = Color.Teal;

                                // Loop through the remaining columns until we reach the stop time column
                                while (startTime < stopTime)
                                {
                                    startTime = startTime.AddMinutes(30);
                                    foreach (DataGridViewColumn col in grdRoom.Columns)
                                    {
                                        if (col.HeaderText == startTime.ToString("HH:mm"))
                                        {
                                            // Set the cell spans for the merged cells
                                            int startColumnIndex = grdRoom.Columns[_HeaderText].Index;
                                            int endColumnIndex = col.Index;
                                            int span = endColumnIndex - startColumnIndex + 1;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.BackColor = Color.Teal;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionBackColor = Color.Teal;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionForeColor = Color.White;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Font = new Font(grdRoom.DefaultCellStyle.Font, FontStyle.Bold);
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Padding = new Padding(0, 0, 0, 0);
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.WrapMode = DataGridViewTriState.True;
                                            // grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionClipBounds = true;//error not run: SelectionClipBounds

                                            // grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionChanged += new EventHandler(grdRoom_SelectionChanged);//error not run: SelectionChanged
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Value = content;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Tag = idLocal;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Selected = true;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].ToolTipText = content + Environment.NewLine + startTime.ToString("HH:mm") + " - " + stopTime.ToString("HH:mm");
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.ForeColor = Color.White;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.BackColor = Color.Teal;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionBackColor = Color.Teal;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionForeColor = Color.White;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Font = new Font(grdRoom.DefaultCellStyle.Font, FontStyle.Bold);
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.Padding = new Padding(0, 0, 0, 0);
                                            grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.WrapMode = DataGridViewTriState.True;
                                            //grdRoom.Rows[rowIndex].Cells[startColumnIndex].Style.SelectionClipBounds = true;//error not run: SelectionClipBounds
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

 


Answers (1)