Search code examples
c#filterradgridradgridview

Filtering Data causing the RowFormatting to be incorrect radgridview - C#


I have a RadGridView which displays a list of products and quantity on order etc, I format the rows in the grid to turn Red if certain conditions are met. This works as it should when the data is loaded. However, I have a text field which filters the data on key press.

When the data is being filtered the RowFormatting stays as it should, when the filter is removed a character at a time however, it is formatting random rows to turn red. After multiple filters are applied and then removed all rows are red.

 void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        radGridView1.Columns["Description"].Width = 300;

        try
        {
            if (e.RowElement.RowInfo.Cells["PT Physical"].Value != null && e.RowElement.RowInfo.Cells["On Order"].Value != null)
            {
                if (Convert.ToInt32(e.RowElement.RowInfo.Cells["On Order"].Value) > (Convert.ToInt32(e.RowElement.RowInfo.Cells["PT Physical"].Value)))
                {
                    e.RowElement.DrawFill = true;
                    e.RowElement.NumberOfColors = 1;
                    e.RowElement.ForeColor = Color.Red;
                    e.RowElement.Font = new Font("Segoe UI", 8, FontStyle.Bold);
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

Above is my RowFormatting function.

 private void txt_search_TextChanged_1(object sender, EventArgs e)
    {
        try
        {
            Console.WriteLine(txt_search.Text);
            CompositeFilterDescriptor searchFilter = new CompositeFilterDescriptor();
            searchFilter.FilterDescriptors.Add(new FilterDescriptor("product", FilterOperator.Contains, txt_search.Text));
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.ShowFilterCellOperatorText = false;
            this.radGridView1.MasterTemplate.FilterDescriptors.Remove("product");

            if (txt_search.Text != "")
            {
                this.radGridView1.MasterTemplate.FilterDescriptors.Add(searchFilter);
            }

        }
        catch (Exception ex)
        {

            MessageBox.Show("Something went wrong searching the grid. Please try again and contact I.T. if this problem persists. \n\n " + ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Above is my text changed event which filters the data displayed in the RGV. Can anyone spot what would be causing the issues with regards to the filter clearing and Rows being formatted incorrectly?


Solution

  • I needed to add an else statement to fix this.

    else
    {
       e.RowElement.DrawFill = true;
       e.RowElement.NumberOfColors = 1;
       e.RowElement.ForeColor = Color.Black;
       e.RowElement.Font = new Font("Segoe UI", 8, FontStyle.Regular);
    }