honag luc

honag luc

  • NA
  • 123
  • 7.6k

Get data from Datagridview2 or database to show Datagridview1

Feb 21 2023 7:46 AM

I have 2 datagridview as shown after adding new data from another form it will save data in datagridview 2. i don't know how to make it go through the columns which are time: start, stop and roomname to get the show data .When it comes to datagridview 1 as shown, it means looking at datagridview 1 to know the meeting time of the meeting room. If you create a button select datarow to select datagridview 2, can load data into datagridview 1 correctly. I want data, I finish clicking add new data from another form, the meeting room data will always be displayed datagrdivew 1. hope everyone can support, and here is the code that can only show 1 line of data.

here code get 1 value:

private void selectAreaColor()
{
    string starttime1 = cboStarttime.Text;
    string stoptime1 = cboStoptime.Text;
    string selectedRoom = cboRoom.SelectedItem.ToString();
   
    DateTime startTime = DateTime.ParseExact(starttime1, "HH:mm", null);
    DateTime endTime = DateTime.ParseExact(stoptime1, "HH:mm", null);
  

    // Loop through the rows in the DataGridView and find the matching row
  
    foreach (DataGridViewRow row in grdMeeting.Rows)
    {
        if (row.Cells[0].Value.ToString() == selectedRoom)
        {
            int rowIndex = row.Index;
            // Loop through the columns and compare the start and end times with the time slots
            for (int i = 1; i < grdMeeting.Columns.Count; i++)
            {
                DateTime columnTime = DateTime.ParseExact(grdMeeting.Columns[i].HeaderText, "HH:mm", null);
                string _HeaderText = grdMeeting.Columns[i].Name.ToString();
                if (startTime == columnTime)
                {
                   
                    grdMeeting.Rows[rowIndex].Cells[_HeaderText].Style.BackColor = Color.Blue;
                   
                    if (startTime == endTime)
                        return;
                    else
                        startTime = startTime.AddMinutes(30);
                }
            }
        }
    }
}


Answers (1)