如何 运行 取决于 Combobox 值并通过 Combobox SelectionChanged 事件调用的方法

How to run Method that depends on Combobox value and called through Combobox SelectionChanged event

我的数据网格上的每个字段都有多个文本框,它根据这些文本框条目对我的数据进行多重过滤。此过滤由一个名为 ApplyFilter() 的方法处理,该方法工作正常。

Datagrid 由来自 MS SQL Server 2008 的查询填充,运行 在我们的网络服务器上。默认情况下,datagrid 选择前 100 个结果来加快速度。我有一个名为 "cboSelectTop" 的组合框,它由数字 10、100、1000、ALL 填充,供用户 increase/decrease 查询结果。

Applyfilter() returns 基于此组合框值的查询。当我从 TextBox 的 TextChanged 事件调用 Applyfilter() 方法时,按预期过滤 运行s。但是,如果我从 Combobox SelectionChanged 事件中调用 ApplyFilter(),我会在 if(!string.IsNullOrEmpty(txtSupplier.Text)) 处得到 System.NullReferenceException,即

Object reference not set to an instance of an object. txtSupplier was Null

问题是:运行Applyfilter() 的变通方法是什么,它取决于组合框的值,以从 TextBox TextChanged 事件和 Combobox SelectionChanged 事件运行?

我的 ApplyFilter() 方法的重要部分是:

   private void ApplyFilter()
    {
        string sConn = @"Data Source=;Initial Catalog=;
                User ID=;Password=;";


        using (SqlConnection sc = new SqlConnection(sConn))
        {
            sc.Open();

            if (!string.IsNullOrEmpty(txtSupplier.Text))
            {
                sql1 = "CompanyName like '" + txtSupplier.Text + "%'and ";
            }
            else
            {
                sql1 = "";
            }
            .
            .
            .
            if (!string.IsNullOrEmpty(txtPrice.Text))
            {
                sql17 = "Price like '" + txtPrice.Text + "%'and ";
            }
            else
            {
                sql17 = "";
            }
            if (cboSelectTop.Text == "ALL")
            {
                sql = "Select * from Priceview Where " + sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7 + sql8 + sql9 + sql10 + sql11 + sql12 + sql13 + sql14 + sql15 + sql16 + sql17;
            }
            else
            {
                sql = "Select  top " + cboSelectTop.Text + " * from Priceview Where " + sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7 + sql8 + sql9 + sql10 + sql11 + sql12 + sql13 + sql14 + sql15 + sql16 + sql17;
            }


            if (sql.Substring(sql.Length - 4) == "and ")
            {
                sql = sql.Remove(sql.Length - 4, 4);
            }
            else if (sql.Substring(sql.Length - 4) == "ere ")
            {
                sql = sql.Remove(sql.Length - 7, 7);
            }
            else
            {
                if (cboSelectTop.Text == "ALL")
                {
                    sql = "Select * from Priceview";
                }
                else
                {
                    sql = "Select top " + cboSelectTop.Text + " * from Priceview";
                }

            }
            Console.WriteLine(sql);

            SqlCommand com = new SqlCommand(sql, sc);

            using (SqlDataAdapter adapter = new SqlDataAdapter(com))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DgPrices.ItemsSource = dt.DefaultView;
            }
        }

    }

任何开始的线索都将不胜感激。

在您尝试在您的方法中访问它们或它们的任何属性之前,只需检查 txtSupplier 和所有其他控件是否已初始化 (!= null):

private void ApplyFilter()
{
    string sConn = @"Data Source=;Initial Catalog=;
                User ID=;Password=;";


    using (SqlConnection sc = new SqlConnection(sConn))
    {
        sc.Open();

        if (txtSupplier != null && !string.IsNullOrEmpty(txtSupplier.Text))
        {
            sql1 = "CompanyName like '" + txtSupplier.Text + "%'and ";
        }
        else
        {
            sql1 = "";
        }
            .
            .
            .
            if (txtPrice != null && !string.IsNullOrEmpty(txtPrice.Text))
        {
            sql17 = "Price like '" + txtPrice.Text + "%'and ";
        }
        else
        {
            sql17 = "";
        }

        if (cboSelectTop == null)
            return;

        if (cboSelectTop.Text == "ALL")
                ...
            }
}

ComboBoxSelectionChanged 事件最初可能会在所有控件初始化之前触发,这就是为什么您会得到 NullReferenceException