代码之家  ›  专栏  ›  技术社区  ›  benjiiiii

过滤数据导致行格式不正确radgridview-C#

  •  0
  • benjiiiii  · 技术社区  · 7 年前

    我有一个RadGridView,它显示产品列表和订单数量等,如果满足某些条件,我将网格中的行格式化为红色。这在加载数据时正常工作。但是,我有一个文本字段,用于过滤按键上的数据。

    过滤数据时,行格式保持不变,但每次删除一个字符时,会将随机行格式化为红色。应用并删除多个过滤器后,所有行均为红色。

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

    上面是我的行格式化函数。

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

    上面是我的文本更改事件,它过滤RGV中显示的数据。是否有人能够发现导致过滤器清除和行格式不正确的问题的原因?

    1 回复  |  直到 7 年前
        1
  •  0
  •   benjiiiii    7 年前

    我需要添加一个else语句来修复此问题。

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