简体   繁体   中英

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.

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

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.

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:

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.

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

like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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