C# 刷新按钮 - 插入、删除、更新后刷新数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14374348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:38:35  来源:igfitidea点击:

Refresh button - Refreshing data grid view after inserting, deleting, updating

c#winformsms-access

提问by Samuel Lee

I'm trying to create a refresh button to automatically refresh the data inside my datagridview after i have finish updating them.

我正在尝试创建一个刷新按钮,以便在完成更新后自动刷新我的 datagridview 中的数据。

However, my refresh button doesn't seem to work. The data displayed remains the same as the original one. It only gets updated after i manually end my windows app and rebuild it.

但是,我的刷新按钮似乎不起作用。显示的数据与原始数据相同。它只有在我手动结束我的 Windows 应用程序并重建它后才会更新。

Here is my code:

这是我的代码:

 private void button_refresh_Click(object sender, EventArgs e)
        {
            this.acuzioSecureStore_DatabaseXDataSet.AcceptChanges();
        }

Please assist. thanks ^_^

请协助。谢谢^_^

采纳答案by Derek

The easiest way to handle this is to use a Binding Source object.

处理此问题的最简单方法是使用 Binding Source 对象。

If your loading information into your DataGridView from an Access Database then your most likely storing the Data in a Dataset or DataTable.

如果您从 Access 数据库将信息加载到 DataGridView 中,那么您最有可能将数据存储在数据集或数据表中。

Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. Then set the Datasource from the DataGridView as the Binding Source object.

创建一个绑定源对象,并在填充数据表/数据集后,将绑定源的数据源设置为数据表。然后将 DataGridView 中的 Datasource 设置为 Binding Source 对象。

Doing this ensures that any changes in your datagridview or reflected in the DataTable and vice Versa. If you reload data into your DataTable it will reflect in the Data Grid Automatically.

这样做可确保您的 datagridview 中的任何更改或反映在 DataTable 中,反之亦然。如果您将数据重新加载到数据表中,它将自动反映在数据网格中。

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

bs.DataSource = dt;

dataGridView1.DataSource= bs;

All changes will now happen automatically.

现在所有更改都将自动发生。

回答by DaitaSFDC

Hey the solution above is good but when the above code is executed,the table disappears and is not seen. And if I execute

嘿,上面的解决方案很好,但是当执行上面的代码时,表格消失了,看不到了。如果我执行

        da.Fill(ds, "p");
        dataGridView1.DataSource = ds.Tables["p"];

then whole table is created again.

然后再次创建整个表。

回答by Naeem Shah

private void button_refresh_Click(object sender, EventArgs e)
        {
            SqlConnection con=new SqlConnection(@"");
            string query="select * from abc";
            SqlCommand cmd=new SqlCommand(query,con);
            SqlDataAdapter da=new SqlDataadapter(cmd);
            DataTable dt=new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource=dt;
        }

回答by Sandman4

I had a datagridview, bound to a table in an Entity Framework database:

我有一个 datagridview,绑定到实体框架数据库中的一个表:

dataGridView1.DataSource = MyDatabase.MyTable;

It would never refresh despite two wasted days. I solved it with a simple workaround:

尽管浪费了两天,但它永远不会刷新。我用一个简单的解决方法解决了它:

private void button_refresh_Click(object sender, EventArgs e) {
    dataGridView1.DataSource = MyDatabase.MyTable.Where(i =>(true));
}

This isan ugly workaround, and friend explained me how it works - if I do just dataGridView1.DataSource = database.table, it will cache the table and use the cached data forever. The fact that each time we create a new dummy query, prevents .net from caching it.

一个丑陋的解决方法,朋友向我解释了它是如何工作的 - 如果我这样做dataGridView1.DataSource = database.table,它将缓存表并永远使用缓存的数据。事实上,每次我们创建一个新的虚拟查询时,都会阻止 .net 缓存它。

回答by karthik

Please try this, it worked for me and let me know if you have any better option.

请试试这个,它对我有用,如果你有更好的选择,请告诉我。

private void button3_Click(object sender, EventArgs e)
{
    dataGridView1.Refresh();
}

回答by user3098137

you Can Bind dataGridView On PageLoad or UserControl Load Event and After chage in grid view Call the Load Event

您可以在 PageLoad 或 UserControl 加载事件上绑定 dataGridView 并在网格视图中更改后调用加载事件

like

喜欢

this.ucUsers_Load(null, null); // Windows From C#

this.ucUsers_Load(null, null); // 来自 C# 的 Windows