简体   繁体   English

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

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

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 ^_^ 谢谢^ _ ^

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中,则最有可能将数据存储在数据集或DataTable中。

Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. 创建一个Binding Source对象,一旦填充了DataTable / Dataset,就可以将绑定源的数据源设置为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. 这样做可确保您的datagridview中的任何更改或反映在DataTable和Versa中。 If you reload data into your DataTable it will reflect in the Data Grid Automatically. 如果将数据重新加载到DataTable中,它将自动反映在数据网格中。

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

bs.DataSource = dt;

dataGridView1.DataSource= bs;

All changes will now happen automatically. 现在所有更改都将自动进行。

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. 然后再次创建整个表。

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

I had a datagridview, bound to a table in an Entity Framework database: 我有一个datagridview,绑定到Entity Framework数据库中的表:

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 is an 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. 一个丑陋的解决方法,朋友向我解释了它是如何工作的 - 如果我只做dataGridView1.DataSource = database.table ,它将缓存表并永远使用缓存的数据。 The fact that each time we create a new dummy query, prevents .net from caching it. 每次我们创建一个新的虚拟查询时,都会阻止.net缓存它。

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

you Can Bind dataGridView On PageLoad or UserControl Load Event and After chage in grid view Call the Load Event 你可以绑定dataGridView在PageLoad或UserControl上加载事件和在网格视图中查看后调用加载事件

like 喜欢

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

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

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