Refresh button - Refreshing data grid view after i

2020-01-29 01:31发布

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

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.

Here is my code:

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

Please assist. thanks ^_^

6条回答
够拽才男人
2楼-- · 2020-01-29 02:14

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-01-29 02:15
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;
        }
查看更多
不美不萌又怎样
4楼-- · 2020-01-29 02:18

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

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

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.

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.

DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

bs.DataSource = dt;

dataGridView1.DataSource= bs;

All changes will now happen automatically.

查看更多
Anthone
5楼-- · 2020-01-29 02:27

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

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. The fact that each time we create a new dummy query, prevents .net from caching it.

查看更多
虎瘦雄心在
6楼-- · 2020-01-29 02:27

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();
}
查看更多
我只想做你的唯一
7楼-- · 2020-01-29 02:35

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

like

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

查看更多
登录 后发表回答