【问题标题】:Updating DataGridView After row search result行搜索结果后更新 DataGridView
【发布时间】:2016-03-30 23:12:02
【问题描述】:

我有一个非常基本的问题,我想使用此代码更新 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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我实际上建议使用 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;
    

    这也是与您所问的类似的另一个答案 How do I Update/Reload DataGridView BindingSource?

    【讨论】:

    • 感谢您的努力。购买我很抱歉这在我目前的情况下没有帮助。我有一个非常简单的问题,我们可以在搜索后使用更新 DataGridView 代码更新 Dataview dv 结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    相关资源
    最近更新 更多