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

  • Question

  • 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.

    • Edited by JC Carmo Monday, August 12, 2019 4:30 PM
    Monday, August 12, 2019 4:16 PM

Answers

  • 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. :)


    jcrcarmo

    • Marked as answer by JC Carmo Tuesday, August 13, 2019 2:30 AM
    Monday, August 12, 2019 10:55 PM

All replies

  • Hello, 

    You didn't say anything about errors you have.

    For example it could be caixaBindingSource not set and remain null. This couse NullPointer exception ob caixaBindingSource.Filter and it cathced and ignored.

    Other option - grid didn't track changes in the source filter. Assign null as a source, than again assign source with new filter. 


    Sincerely, Highly skilled coding monkey.

    Monday, August 12, 2019 4:32 PM
  • My thought, it doesn't fire because nothing has triggered it (cell value changed and needs formatting vs a filter, nothing has changed)

    Perhaps try using CellValueChanged event.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, August 12, 2019 5:13 PM
    Moderator
  • 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. :)


    jcrcarmo

    • Marked as answer by JC Carmo Tuesday, August 13, 2019 2:30 AM
    Monday, August 12, 2019 10:55 PM
  • Hi JC Carmo,

    Thank you for posting here.

    I am glad that your problem has been solved. 

    If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 13, 2019 2:27 AM
    Moderator