C# 2015 - DataGridViewCellFormatting event ok on form load, but not working properly on textchange event

  • Thread starter Thread starter JC Carmo
  • Start date Start date
J

JC Carmo

Guest
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

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)
{
}
}

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.

Continue reading...
 
Back
Top