Search code examples
c#visual-studio

showing color in datagridview with the value of calendar


I tried to fix my logical error in my datagridview. Here my code.

while (dr.Read())
{

    int id = dr.GetInt32(0);
    DateTime startBook = dr.GetDateTime(2);
    DateTime endBook = dr.GetDateTime(3);

    int startBook_C = startBook.Day;
    int endBook_C = endBook.Day;

    int TotalDays = endBook_C - startBook_C;

    //dataGridView1(startBook_C, id - 1).Style.BackColor = Color.Blue;
    dataGridView1.Rows[startBook_C].Cells[endBook_C-1].Style.BackColor = Color.Blue;
    int i;
    for (i = 1; i <= TotalDays; i++)
    {
        dataGridView1.Rows[startBook_C + i].Cells[endBook_C -1].Style.BackColor = Color.Blue;
        //dataGridView1.Rows[startBook_C].Cells[carID - 1].Style.BackColor = Color.Blue;
        // dataGridView1(startBook_C + i, id - 1).Style.BackColor = Color.Red;/* TODO ERROR: Skipped SkippedTokensTrivia */
        TotalDays -= 1;
    }
}
Con.Close();

I have a problem with datagridview, it's logical error and I don't know what is my error. I have an image below:

enter image description here

enter image description here


Solution

  • Consider startBook.Day= day 1 and endBook.Day = day 3 ( the row of ID 23). And the first loop i =1;

    Then dataGridView1.Rows[startBook_C + i].Cells[endBook_C -1] is dataGridView1.Rows[**2**].Cells[**2**](start from 0) so the image provided shows DataGrid view table[3][3] is color blue .

    For ID 23, the row is 0 (the first row). cell starts from 4 (the fifth, offset is 4, //ID+CARID+book StartDay+book EndDay=4.)

    TotalDays -= 1; is not required in the loop.

    while (dr.Read())
    {
        int offset = 4;//ID+CARID+book StartDay+book EndDay=4.
        int id = dr.GetInt32(0);
        DateTime startBook = dr.GetDateTime(2);
        DateTime endBook = dr.GetDateTime(3);
    
        int startBook_C = startBook.Day;//1
        int endBook_C = endBook.Day;//3
    
        int TotalDays = endBook_C - startBook_C; //2
       
        for (int i = 0; i <= TotalDays; i++)
        {
           dataGridView1.Rows[0].Cells[offset+startBook_C - 1+i].Style.BackColor = Color.Blue;
          
        }
    
    }
    Con.Close();
    

    Update: Since many methods in the examples do not provide clear definitions, I've provided my own way

    int offset = 4;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       if (!row.IsNewRow)
       {
    
           if (row.Cells.Count >= 4)
           {
    
               DateTime startBook;
               DateTime endBook;
               if (DateTime.TryParse(row.Cells[2].Value?.ToString(), out startBook) &&
                   DateTime.TryParse(row.Cells[3].Value?.ToString(), out endBook))
               {
    
                   int startBook_C = startBook.Day;
                   int endBook_C = endBook.Day;
                   int TotalDays = endBook_C - startBook_C;
                   for (int i = 0; i <= TotalDays; i++)
                   {
                       dataGridView1.Rows[row.Index].Cells[offset + startBook_C - 1 + i].Style.BackColor = Color.Blue;
    
                   }
               }
           }
       }
    }
    

    enter image description here