• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2661
  • Last Modified:

Refresh DataGridView

I am trying to refresh a DataGridView with a bindingsouce to an Access table.  The table is changed and my DataGridView is not reflecting the changes.

I've tried:

dgv.Refresh();

If I exit the form and restart, then I can see the changes in the table in the DataGridView.

How do I tell the DataGridView to refresh as I change the table?
0
rrmart
Asked:
rrmart
  • 6
  • 3
  • 2
  • +1
2 Solutions
 
AUmidhCommented:
You have to get the data and then refresh datagridview
0
 
rrmartAuthor Commented:
Thanks for the reply AUmidh.  What exactly do you mean by "get"?  

I can see the original table data in the DataGridView fine when the form loads.  Then I change the table in the code behind the form, but don't see these changes when done.

0
 
Aleksei_MalkovCommented:
The simpilest way is to reset DataSource

gridView.DataSource = null;
gridView.DataSource = table;

but i wouldn't say it is the best way.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
AUmidhCommented:
@rrmart:
Recall the procedure which you implement on form load before dgv.Refresh();
0
 
rrmartAuthor Commented:
I tried:

                dgv.DataSource = null;
                dgv.DataSource = dgBindingSource;
                dgv.Refresh();

This did not work either.  Hm-m-m-m  I noticed though, that this caused the first two columns in the DataGridView to go blank (ID (primary key), and the next field).

Thanks for the idea, though.

0
 
abelCommented:
You're on the wrong track here (sorry to interrupt). The .Refresh() method just invalidates the client area and redraws the content. It won't do much in this scenario, it's inherited from the Control base class and every control has it.

What you need is, quote from this post: "Set your users collection to BindingSource's DataSource property and DataGridView's DataSource to the BindingSource instance, something like in the following code example:"

Alternatively, you can call ResetBindings on the binding source, which should also work.


0
 
rrmartAuthor Commented:
Thanks abel.  I still cannot get this to work.  I suspect my bindingsource is not what I think it is:

Tried:
bindingSource1.DataSource = propertyDBDataSet3._InventoryTempTable;
dgv.DataSource = bindingSource1;

In Design View I can preview bindingSource1 and the data shows correctly.

Is there a DataGridView property setting I'm missing?  (I've cleared the DataGridView and started over numerous times now.)
0
 
abelCommented:
Did you try bindingSource1.ResetBindings()? What is the class type of the binding source?
0
 
rrmartAuthor Commented:
Yes I tried:

bindingSource1.ResetBindings(false);   //  Also tried true (not changing schema though)

bindingSource1.DataSource = propertyDBDataSet3._InventoryTempTable;
dgv.DataSource = bindingSource1;

The class type of my bindingSource1 is:

System.Windows.Forms.BindingSource


Thanks abel for hanging in here with me.   . . .
0
 
rrmartAuthor Commented:
I finally solved this:

gdv.DataSource = bindingSource1;
                _InventoryTempTableAdapter.Fill(this.propertyDBDataSet3._InventoryTemp);

Where propertyDBDataSet3 ==> DataSet
_InventoryTemp                    ==> Datamember

I had to refill the table adapter.  Not sure why but it worked.
0
 
rrmartAuthor Commented:
Your comment lead me to investigate DataSet and Tableadapters I was using and declaring at form load time.
0
 
abelCommented:
I will remember that solution, thanks for pointing that out. For asp.net, I knew that (and it has Rebind method), I thought it wasn't necessary on WinForms. Glad you found your solution in the end. ;-)
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now