简体   繁体   English

在行搜索结果之后更新DataGridView

[英]Updating DataGridView After row search result

I have a very basic question I want to update DataGridView using this code 我有一个非常基本的问题,我想使用此代码更新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);
        }

After search for a result using this code 使用此代码搜索结果后

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

After getting result of search I couldn't been able to update searched result. 获取搜索结果后,我无法更新搜索结果。 updateDGV1_Click works fine on the whole DGV but not on Searched result like in below image After search,result not updating updateDGV1_Click在整个DGV上工作正常,但在下图所示的搜索结果上却无法正常工作搜索后,结果未更新

I would actually recommend using the TableAdapter to communicate with your database instead of generating a connection in that fashion. 我实际上建议使用TableAdapter与您的数据库进行通信,而不是以这种方式生成连接。 This will also allow you to easily update your DataGridView with the contents of the TableAdpater Query (Search) that you want to preform. 这也使您可以轻松地使用要执行的TableAdpater查询(搜索)的内容来更新DataGridView。 I wish I could go into more detail but I'm in a hurry at the moment I will provide links explaining this better below. 我希望我可以进一步详细介绍,但目前我急于在下面提供一些链接,以更好地说明这一点。

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

I personally use TableAdapters to connect databases in my projects, but I have found a solution that may also allow you to keep your code the way it is for the most part. 我个人使用TableAdapter在我的项目中连接数据库,但是我发现了一个解决方案,该解决方案还可以使您在大多数情况下保持代码原样。

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

What you will need to do when you want to preform a search on the current working DataSet. 要在当前工作的数据集上进行搜索时需要执行的操作。 This code is an example I haven't tested it. 此代码是我尚未测试的示例。

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;

Here is also another answer on SO similar to what you're asking How do I Update/Reload DataGridView BindingSource? 这也是关于SO的另一个答案,类似于您所要的内容如何更新/重新加载DataGridView BindingSource?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM