Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# Conditional formatting of DataGridView cells

$
0
0

Hello everyone,

Greetings from Brazil!  I have a DataGridView with a column named "Valor" that displays a currency amount in Brazilian Real (R$ and -R$).  I am trying to make the cell ForeColor Red if the amount is negative, and Blue if the amount is positive.  I've tried the code below, but it's not working.  What am I doing wrong?  Please help!  I appreciate your time and expertise.  Thank you!

        private void tbl_caixaDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (int i = 0; i < tbl_caixaDataGridView.Rows.Count; i++)
            {
                if (Convert.ToInt32(tbl_caixaDataGridView.Rows[i].Cells["Valor"].Value) < 0)
                {
                    tbl_caixaDataGridView.Rows[i].Cells["Valor"].Style.ForeColor = Color.Red;
                }
            }
        }

Best regards,

JC Carmo :)


jcrcarmo


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>