0

我有一个 WPF 浏览器应用程序,它获取存储在 SQL Server 上的数据,将其存储在 DataTable 中并将其显示在 DataGrid 中。现在我想要一个文本框,您可以在其中搜索 DataTable 中的条目,但是当我加载应用程序时,我收到一条错误消息,告诉我找不到 [Company] 行。

我认为问题在于,当将过滤器应用于 DataTable 时,DataTable 尚未填充。有人可以给我一个提示如何使它工作吗?

DataTable dt = new DataTable();

public Page1()
{
    InitializeComponent();
    showSQLData();
}

private void showSQLData() 
{
    string sqlConnectionString = @"blabla";
    string sqlCommandString = "SELECT * FROM Excel_import";

    using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
    {
        SqlCommand cmd = new SqlCommand(sqlCommandString, sqlConnection);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        adapter.Fill(dt);

        dataGridSQLData.ItemsSource = dt.DefaultView;
    }
}

private void textBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
{
    dt.DefaultView.RowFilter = string.Format("Company LIKE '%{0}%'", textBoxSearch.Text);
}
4

1 回答 1

0

根据您的最新评论,我猜想这是在通话textBoxSearch_TextChanged中被解雇的。如果不满足该条件InitializeComponent(),您可以签dt.Rows.Count > 0入并返回。textBoxSearch_TextChanged

于 2013-02-10T17:09:48.277 回答