when I have a datagridview and I need to compare a cell with a number and then color the cell accordingly
private void DGV2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) > 96.69)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.LightGreen;
}
if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) > 94)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.Yellow;
}
if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) < 94)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.Red;
}
}
it's not showing me correctly
CodePudding user response:
you need
if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) > 96.69)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.LightGreen;
} else if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) > 94)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.Yellow;
}
else if (Convert.ToInt32(DGV2.Rows[11].Cells[3].ColumnIndex) < 94)
{
DGV2.Rows[11].Cells[3].Style.BackColor = Color.Red;
}
CodePudding user response:
I already found a way to do it, thank you very much for the answers, they helped me, I typed double and had the wrong input format (shame on me) thank you very much, I am showing how I solved it, in case it could help someone.
private void DGV3_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow selectedRow = DGV3.Rows[11];
label3.Text = (selectedRow.Cells[3].Value.ToString());
label3.Text = label3.Text.Replace("%", "");
double someDouble = 96.69;
int y = Convert.ToInt32(someDouble);
double d = Convert.ToDouble(label3.Text, CultureInfo.InvariantCulture);
int vstup = Convert.ToInt32(d);
if (vstup > y)
{
DGV3.Rows[11].Cells[3].Style.BackColor = Color.LightGreen;
}
if (vstup >= 94)
{
DGV3.Rows[11].Cells[3].Style.BackColor = Color.Yellow;
}
if (vstup < 94)
{
DGV3.Rows[11].Cells[3].Style.BackColor = Color.Red;
}