Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

Greetings from Brazil!  I'm a beginner programmer developing a Visual Studio 2015 C# WinForms database application with MySQL.

I have a DataGridViewCellFormatting event on the form and it works perfectly when the form loads, but when the DataGridView is updated triggered by a TextChanged event to search for records, the cell formatting event doesn't work properly anymore.

If the value of column 4 is negative, than the row forecolor should be red and if the value is positive the row forecolor should be blue, as it happens when the form loads

I've tried Update, Refresh, Invalidate and everything else on the book, but it doesn't work.  Thank you.  I really appreciate your time and help!

Best regards,

JC.


What I have tried:

Here's my code:

        private void caixaDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            caixaDataGridView.RowsDefaultCellStyle.BackColor = Color.White;
            caixaDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;

            try
            {
                decimal valor = Convert.ToDecimal(bremi691_appDataSet.caixa.Rows[e.RowIndex]["valor"]);

                if (valor < 0)
                {
                    e.CellStyle.ForeColor = Color.Red;
                }
                else
                {
                    e.CellStyle.ForeColor = Color.Blue;
                }
            }
            catch (Exception)
            {
            }
        }


And for the TextChanged event:

        private void txtSearch_TextChanged(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (cboColumn.Text == "Nome")
                {
                    caixaBindingSource.Filter = string.Format("nome LIKE '{0}%'", txtSearch.Text);
                }

                if (cboColumn.Text == "Tipo")
                {
                    caixaBindingSource.Filter = string.Format("tipo LIKE '{0}%'", txtSearch.Text);
                }

                if (cboColumn.Text == "Referência")
                {
                    caixaBindingSource.Filter = string.Format("referencia LIKE '{0}%'", txtSearch.Text);
                }
            }
            catch (Exception)
            {
            }
        }
Posted
Updated 12-Aug-19 12:58pm

1 solution

I've found the solution! It had absolutely nothing to do with binding sources or the like. All I had to do was change my CellFormatting code to the following:

private void caixaDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
caixaDataGridView.RowsDefaultCellStyle.BackColor = Color.White;
caixaDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;

foreach (DataGridViewRow MyRow in caixaDataGridView.Rows)
{
if (Convert.ToInt32(MyRow.Cells[4].Value) < 0)
{
MyRow.DefaultCellStyle.ForeColor = Color.Red;
}
else
{
MyRow.DefaultCellStyle.ForeColor = Color.Blue;
}
}
}

Now it works perfectly! But I thank you guys very much for your time and attempt to help me. I really appreciate it!

Best regards,

JC. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900