Link to home
Start Free TrialLog in
Avatar of smaguire
smaguireFlag for Canada

asked on

Refreshing DataGridView Control

hi all,
I have a DataGridView Control that is bound to a dataset and the ds linked to a stored procedure
the DGV is placed in a tab control, whenever the user clicks that tab, I call the following method:
private void InitializeDisplaySettingsGrid()
        {
            dgvDisplaySettings.DataSource = null;
            dgvDisplaySettings.DataSource = ds.CustomDisplaySettings.DefaultView;
        }
it works and show data just fine, but if I change anything in the table (from the database or from the application) the new modified data will not show until the I stop and restart the application.
I tried to use dgvDisplaySettings.Refresh() but didn't help, I also tried manually as you can see from the code above to set the datasource to null and then call it again, without any luck

Why can't I get it to show the modified data IF i am binding it to the datasource everytime the user selects that tab page?
I am using windows forms, and visual studio 2008

thanks
Avatar of ororiole
ororiole
Flag of United States of America image

use
dgvDisplaySettings.DataSource = ds.CustomDisplaySettings.DefaultView;
dgvDisplaySettings.DataBind();

Binding doesnt actually occur until the DataBind event, which you can ensure by calling DataBind() anytime your datasource changes. If your database table changes, you are going to need to go get it again and update your datatable. (At least, thats the basic way)
Avatar of smaguire

ASKER

No DataBind() property in windows form
Ah. In that case call Refresh in the DataBindingComplete event
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
           dataGridView1.Refresh();
        }

Open in new window

Thanks ororiole for your reply,
I've tried that and still does not work. I can verify data being changed in the database but will not show in the grid until i restart the app

thanks
SEt a breakpoint in the DAtaBindComplete event handler to make sure it is bering raised. Then take a look at the DataBindingCompelteEventArgs to see if the CompleteState is Success and/or what ErrorText may be.

Make sure you are reloading your dataset/datatable when the database changes.
ASKER CERTIFIED SOLUTION
Avatar of smaguire
smaguire
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial