【问题标题】:Refresh datagridview win forms after updating the database from a child form从子表单更新数据库后刷新 datagridview win 表单
【发布时间】:2012-06-08 22:54:29
【问题描述】:

从另一个表单对数据库进行更改后如何刷新datagridview, 关闭子窗体后,我尝试使用单击事件刷新 datagridview,但它不起作用,我必须使用数据集吗?

            //create an OleDbDataAdapter to execute the query
            dAdapter = new OleDbDataAdapter(gQuery, connString);

            //create a command builder
            cBuilder = new OleDbCommandBuilder(dAdapter);

            //create a DataTable to hold the query results
            dTable = new DataTable();

            //fill the DataTable
            dAdapter.Fill(dTable);


            //BindingSource to sync DataTable and DataGridView
            bSource = new BindingSource();

            //set the BindingSource DataSource
            bSource.DataSource = dTable;


            //set the DataGridView DataSource
            dataGridView1.DataSource = bSource;

    private void button_Refresh_Click(object sender, EventArgs e)
    {

        dataGridView1.DataSource = bSource;
        dataGridView1.Refresh();


    }

帮帮我,请提前谢谢

【问题讨论】:

标签: c# datagridview dataset dataadapter


【解决方案1】:

你试过了吗

dataGridView1.DataSource = dTable;

【讨论】:

    【解决方案2】:

    添加

    dataGridView1.Update();
    

    它会解决你的问题。

    【讨论】:

      【解决方案3】:
       bSource.DataSource = dTable;
       dataGridView1.DataSource = bSource;
      

      如果你能回忆起你的桌子会更好

      【讨论】:

        【解决方案4】:

        当您将数据库与 DataGridView 属性中的“DataSource”链接时,IDE 会自动将 BindingSource 和 TableAdapter 添加到您的表单中。

        如果数据库已更新并且您想刷新 DataGridView,请调用:

        this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>);
        

        其中&lt;table name&gt; 是您的表名(例如Users),&lt;DB name&gt; 是您的数据库名(例如MyDB)。

        this.UsersTableAdapter.Fill(this.MyDBDataSet.Users);
        

        【讨论】:

        • 很好的答案。我一直在寻找这个。谢谢!
        猜你喜欢
        • 1970-01-01
        • 2014-10-09
        • 2011-09-13
        • 1970-01-01
        • 2016-07-25
        • 2012-05-25
        • 2015-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多