Refresh datagridview win forms after updating the database from a child form
Asked Answered
K

4

5

how to refresh datagridview after making changes on the database from another form, after closing child form i tried to refresh the datagridview with click event but it's not working, do i have to use dataset ?

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


    }

Help me, please thanks in advance

Kalman answered 4/6, 2012 at 21:21 Comment(2)
possible duplicate of simple DataGridView refresh questionDearth
#9791176Dearth
K
9

Add

dataGridView1.Update();

It will solve your problem.

Kiona answered 15/6, 2012 at 12:26 Comment(0)
A
7

When you link your database with "DataSource" in DataGridView properties, IDE automatically adds BindingSource and TableAdapter to your form.

If database is updated and you want to refresh DataGridView, call this:

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

Where <table name> is name of your table (for example Users) and <DB name> is name of your database (for example MyDB).

this.UsersTableAdapter.Fill(this.MyDBDataSet.Users);
Avast answered 24/7, 2013 at 14:5 Comment(0)
P
0

have you tried

dataGridView1.DataSource = dTable;
Puebla answered 4/6, 2012 at 21:26 Comment(0)
H
0
 bSource.DataSource = dTable;
 dataGridView1.DataSource = bSource;

it would be better if you will recall your table

Humphrey answered 12/3, 2013 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.