在行搜索结果

时间:2016-03-30 14:33:34

标签: c# wpf

我有一个非常基本的问题,我想使用此代码更新DataGridView

private void updateDGV1_Click(object sender, EventArgs e)
    {
        SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
        // open connection to database 
        try
        {

            cmbl1 = new SQLiteCommandBuilder(datadp1);
            datadp1.Update(ds1, "PlotDetails");
            MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            load_table();
            AutoCompleteSizeSearch();
            AutoCompletePlotSearch();
            AutoCompleteOwnerSearch();
            AutoCompleteLocatoinSearch();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

使用此代码搜索结果后

  private void plots_txt_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt1);
        dv.RowFilter = string.Format("D_ID LIKE '%{0}' AND Area LIKE '%{1}' AND Cat LIKE '%{2}' AND PBS LIKE '%{3}%' AND Name LIKE '%{4}%' AND Size LIKE '%{5}%' AND Location LIKE '%{6}%' AND PlotNo LIKE '%{7}%'", dids_combo.Text, areacs_txt.Text, categorycs_txt.Text, phblses_txt.Text, owners_txt.Text, sizes_txt.Text, locations_txt.Text, plots_txt.Text);
        dataGridView1.DataSource = dv;
    }

获得搜索结果后,我无法更新搜索结果。 updateDGV1_Click在整个DGV上工作正常,但在搜索结果上没有,如下图所示 After search,result not updating

1 个答案:

答案 0 :(得分:0)

我实际上建议使用TableAdapter与您的数据库进行通信,而不是以这种方式生成连接。这还允许您使用要执行的TableAdpater查询(搜索)的内容轻松更新DataGridView。我希望我能够详细介绍,但我现在很着急,我将在下面提供更好的解释。

https://msdn.microsoft.com/en-us/library/bz9tthwx.aspx

我个人使用TableAdapters连接我的项目中的数据库,但我找到了一个解决方案,也可以让你保持代码的大部分时间。

http://www.codeproject.com/Articles/14249/How-to-populate-DataGridView-GridView-with-SQL-sta

当您想要在当前工作的DataSet上执行搜索时,您需要做什么。这段代码就是我没有经过测试的例子。

string conn_str = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
string sql_str = “select * from table1”;

SqlDataAdapter data_adapter = new SqlDataAdapter(sql_str, conn_str);
SqlCommandBuilder cmd_builder = new SqlCommandBuilder(data_adapter);

// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
// This line populates our new table with the data from our sql query
data_adapter.Fill(table);
db_binding_source.DataSource = table;

// Resize the DataGridView columns to fit the newly loaded content.
data_grid_view.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
// you can make it grid readonly.
data_grid_view.ReadOnly = true; 
// finally bind the data to the grid
data_grid_view.DataSource = db_binding_source;

这也是SO的另一个答案,类似于你提出的问题 How do I Update/Reload DataGridView BindingSource?