1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var voitures = new List<Voiture>
{
new Voiture { Nom="Abc", NombrePorte=3},
new Voiture { Nom="Def", NombrePorte=5},
new Voiture { Nom="Ghj", NombrePorte=2},
new Voiture { Nom="Klm", NombrePorte=6},
new Voiture { Nom="Nop", NombrePorte=2},
new Voiture { Nom="Qrs", NombrePorte=3},
new Voiture { Nom="Tuv", NombrePorte=5},
};
dgvTest.DataSource = voitures;
this.dgvTest.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvTest_CellFormatting);
}
private void dgvTest_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = dgvTest.Rows[e.RowIndex];
int nombrePorte = (int)row.Cells[1].Value;
if (nombrePorte > 4)
e.CellStyle.BackColor = Color.Green;
else
e.CellStyle.BackColor = Color.Red;
}
}
public class Voiture
{
public string Nom { get; set; }
public int NombrePorte { get; set; }
} |
Partager