Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I'm tired to fix my logical error in my datagridview, can someone please help me. 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;


What I have tried:

I have a problem with datagridview, it's logical error and I don't know what is my error that why I ask here and attention to all master to help to fix my logical error in my datagridview. I have a image below, please visit the link to see what I mean.

Dave Kreskowiak 1-May-24 22:14pm    
So did you want to describe what you mean by "logical error", or should everyone just guess?

Your image link is useless as only you can see the content.
ras bry 2-May-24 0:17am    
Dave Kreskowiak Good day,

What I mean is my datagridview problem.
Dave Kreskowiak 2-May-24 9:39am    

OK, first off your image link gives "Access denied" so we can't gain any information from that and we have no idea what "logical error" you might be talking about: we have no access to your code when running with your data, no way to look at your screen, and no idea what you expect the code to do other than the nebulous "colour my background".

Colouring the background of a DGV is pretty easy - I do it in the CellPainting event so it only applies to displayed cells: Colouring DataGridView Cells According to their Content in WinForms[^] may help you.

If not, then you need to be a lot more forthcoming about your problem: we can't read your mind! So read this: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.
Share this answer
You already asked a variation of this question here[^] and, as you have more context there, you should continue there. As I explained in my reply to you there, you need to use the index of the row and the index of the cell that you want to change colour for.

As you aren't showing anyone what you want your end state needs to be, you are going to have to work out what those indexes are for yourself. To be honest, that shouldn't be that difficult for you - all you need to do is step through the code and watch the values while you are doing it. This is straightforward debugging and shouldn't take you long to figure out for yourself, but you have to make the effort.
Share this answer
Thank you to all everyone for your support Dave Kreskowiak, OriginalGriff, Pete O'Hanlon

My assignment is already finish now.
Thank you so much.

Image ouput
Share this answer
Pete O'Hanlon 3-May-24 1:02am    
Congratulations. I'm pleased that you managed to get it working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900