DataSet dataset=new DataSet(); SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=TEST;Integrated Security=True"); SqlDataAdapter adap = new SqlDataAdapter(); adap.SelectCommand = new SqlCommand("SELECT TOP 3 * FROM Matériel WHERE Date_entretien >= GETDATE() ORDER BY Date_entretien;", con); adap.Fill(dataset); dataGridView1.DefaultCellStyle.BackColor = Color.Red;// ici on force la couleur par défaut pour toutes les lignes dataGridView1.DataSource = dataset.Tables[0];
int ColumnToCheck = -1;
this.dataGridView1.RowPrePaint += new System.Windows.Forms.DataGridViewRowPrePaintEventHandler(this.dataGridView1_RowPrePaint);
// DataSet dataset=new DataSet(); SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=TEST;Integrated Security=True"); SqlDataAdapter adap = new SqlDataAdapter(); adap.SelectCommand = new SqlCommand("SELECT * FROM Matériel ORDER BY Date_entretien;", con); adap.Fill(dataset); ColumnToCheck = dataset.Tables[0].Columns.IndexOf("Date_entretien"); // on recherche l'index de la colonne à tester dataGridView1.DataSource = dataset.Tables[0];
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (ColumnToCheck !=-1 && dataGridView1.Rows[e.RowIndex].Cells[ColumnToCheck].Value != null) if ((DateTime)dataGridView1.Rows[e.RowIndex].Cells[ColumnToCheck].Value >= DateTime.Now.Date) dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; else dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; }
dataGridView1.Rows[RowIndex].DefaultCellStyle.BackColor = Color.Aquamarine;
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) if ((int)dataGridView1.Rows[e.RowIndex].Cells[0].Value % 2 == 0) dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LavenderBlush; else if ((int)dataGridView1.Rows[e.RowIndex].Cells[0].Value % 3 == 0) dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Lavender; else dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; }Bob.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question