Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Attention to all, I need your help I'm using c#. The error message said, the datagridview1 cannot be used like a method. Please help me to fix this issue.

here's my code.

private void ShowSchedule()//show color
        {
            string ConString = "server=localhost;user=root;database=carrental;password=;";
            MySqlConnection Con = new MySqlConnection(ConString);
            Con.Open();

            string query = "Select * from rent_schedule Where startBook >= @ST AND endBook <=@ET";

            DateTime startDate = new DateTime(monthCalendar1.SelectionStart.Year, monthCalendar1.SelectionStart.Month, 1);
            DateTime EndDate = startDate.AddMonths(1);

            MySqlCommand cmd = new MySqlCommand(query, Con);

            cmd.Parameters.AddWithValue("@ST", startDate);
            cmd.Parameters.AddWithValue("@ET", EndDate);

            MySqlDataReader dr = cmd.ExecuteReader();

            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[2].Cells[0].Style.BackColor = Color.Blue;

                int i;
                for (i = 1; i <= TotalDays; i++)
                {
                    dataGridView1(startBook_C + i, id - 1).Style.BackColor = Color.Red;/* TODO ERROR: Skipped SkippedTokensTrivia */
                    TotalDays -= 1;
                }


            }
            Con.Close();
        }


What I have tried:

now my expecting output like this, Please click the link image below.

https://i.sstatic.net/3Xvqg9lD.png[^]

My problem is I do not know how to fix my datagridview error. Thank you so much who can help me or fix my problem.
Posted

1 solution

Your problem is on this line:
C#
dataGridView1(startBook_C, id - 1).Style.BackColor = Color.Blue
As you can see, this looks like a method call. Are you trying to do something like this?
C#
dataGridView1.Rows[startBook_C].Cells[id-1].Style.BackColor = Color.Blue;
 
Share this answer
 
Comments
ras bry 1-May-24 10:41am    
Good day Pete O'Hanlon, It's already display a color. Thank you so much for your help, but we have some problem. Please visit the link to view the image.

https://drive.google.com/file/d/1rmKdYzKhJnfTm4NNQR7dev7Gg0jCIZM7/view?usp=sharing

I don't where is the mistake of my codes.
Pete O'Hanlon 1-May-24 10:55am    
So, you corrected the error that you were seeing before and the code compiles and runs. I know you have shared a picture but that doesn't tell me what error you are seeing now.
ras bry 1-May-24 11:09am    
Pete O'Hanlon good day,

The error sir is we displaying a color in deferent cell of datagridview not the cell of Book start columns and Book end columns.
Pete O'Hanlon 1-May-24 11:18am    
That's happening because you are using the id field in your calculation of the cell and startBook_C for the row. You need to correct your logic to use the index of the cell, and the index of the row. You should be able to work this out for yourself - attaching the debugger and stepping through this logic would have told you that.
ras bry 1-May-24 20:35pm    
Good day sir Pete O'Hanlon,

Can you help me sir, what is the best logic to fix this problem.

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