Question

I have one data-grid view which is bind dynamically. and it contain data like below screen shot.

Data-Grid View

Now problem is i want change cell color based on data value. i want to deduct tamount-paymentamont and if it >=1 then i want to set this two cell color as a red and other as green.at every new data bind.

I try this Answer but not work for me.

Was it helpful?

Solution

I try this at bind time

  for (int n = 0; n < (dataGridView1.Rows.Count - 1); n++)
            {
                double i = Convert.ToDouble(dataGridView1.Rows[n].Cells["tamount"].Value.ToString().Replace('.', ','));
                double j = Convert.ToDouble(dataGridView1.Rows[n].Cells["paymentamount"].Value.ToString().Replace('.', ','));
                double total = i - j;
                if (total >= 1)
                {
                    dataGridView1.Rows[n].Cells["tamount"].Style.BackColor = Color.LightPink;
                    dataGridView1.Rows[n].Cells["paymentamount"].Style.BackColor = Color.LightPink;

                }
                else
                {
                    dataGridView1.Rows[n].Cells["tamount"].Style.BackColor = Color.LightGreen;
                    dataGridView1.Rows[n].Cells["paymentamount"].Style.BackColor = Color.LightGreen;
                }

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top