private void dgv_DataSourceChanged(object sender, EventArgs e) { for (int i = 0; i < dgv.Rows.Count; i++) { string CY = (Decimal.Parse(dgv.Rows[i].Cells["MESLPSL"].Value.ToString()) + Decimal.Parse(dgv.Rows[i].Cells["MESFPSL"].Value.ToString() == "" ? "0" : dgv.Rows[i].Cells["MESFPSL"].Value.ToString()) - Decimal.Parse(dgv.Rows[i].Cells["SAPLPSL"].Value.ToString() == "" ? "0" : dgv.Rows[i].Cells["SAPLPSL"].Value.ToString()) - Decimal.Parse(dgv.Rows[i].Cells["SAPFPSL"].Value.ToString() == "" ? "0" : dgv.Rows[i].Cells["SAPFPSL"].Value.ToString())).ToString(); dgv.Rows[i].Cells["CY"].Value = CY; if (CY != "0") { dgv.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Red; } else { dgv.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Green; } } }