Good day everyone, I make a calendar booking using datagridview. Please take a look the image below.
as you can see they have Book start and Book end. Now I want to put a color from date start to date end. Please take a look the image below.
I don't how to put the color in datagridview, I hope someone can help me to fix this problem.
- I have a code in Book car button and here the code.
if (textBox1.Text == "")
{
MessageBox.Show("textbox empty.");
} else if (textBox1.Text != "")
{
string conStr = "server=localhost;user=root;database=carrental;password=;";
MySqlConnection conn = new MySqlConnection(conStr);
conn.Open();
string dt1 = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
string dt2 = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
string query = "insert into rent_schedule (carID,startBook,endBook) Values ('" + textBox1.Text + "','" + dt1 + "','" + dt2 + "')";
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.ExecuteReader();
MessageBox.Show("done");
con.Close();
dataGridView1.Columns.Clear();
Add_Columns();//call the module
}
- And this is for the module,
private void Add_Columns()
{
string connection = "server=localhost;user=root;database=carrental;password=";//filter all dtgAllSTock //LOCAL CONNECTION
string query = "SELECT * FROM rent_schedule";
MySqlConnection con = new MySqlConnection(connection);
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].Width = 200;
dataGridView1.Columns[1].Width = 550;
dataGridView1.Columns[2].Width = 500;
//dataGridView1.Columns[3].Width = 900;
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[1].HeaderText = "Car ID";
dataGridView1.Columns[2].HeaderText = "Book start";
dataGridView1.Columns[3].HeaderText = "Book end";
dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
////
///
//dataGridView1.Columns.Add("carID", "Car ID");
int DayInMonths = DateTime.DaysInMonth(monthCalendar1.SelectionStart.Year, monthCalendar1.SelectionStart.Month);
int i;
for (i = 1; i <= DayInMonths; i++)
dataGridView1.Columns.Add("{0}", i.ToString());
}
- And this my code for displaying color, but I think this code something wrong.
private void ShowCollors()//show label rent
{
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(1);
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.Red;
int i;
for (i = 1; i <= TotalDays; i++)
{
//dataGridView1(startBook_C, id - 1).Style.BackColor = Color.Red;/* TODO ERROR: Skipped SkippedTokensTrivia */
TotalDays -= 1;
}
}
Con.Close();
}