Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridView not being refreshed when data changes
 

DataGridView not being refreshed when data changes

Hi,

I have a form w/ three DataGridViews (dgvInterivews, dgvDemarcationSets & dgvCodingSets), all related in a parent-child-grandchild kind of relationship. There are three BindingSources providing the data sources. All works great.

A button on the form (actually a button contained in a user control on the form)fires offa database update which adds a record to the middle DataGridView, dgvDemarcationSets. I want dgvDemarcationSets to refresh itself and show the new row.

The user control whch contains the button, called ucButtons, raises an event which is handled by the containing form.I can see that this is happening properly, but DataGridView is not refreshing. Here is the code on the containing form:

<pre lang="x-c#">void ucButtons_ButtonClickEvent(object sender, EventArgs e)
{
	WriteLog("dlgBrowseInterviews", "ucButtons_ButtonClickEvent", true, true);
	bsInterviews.ResetBindings(false);
	bsDemarcationSets.ResetBindings(false);
	bsCodingSets.ResetBindings(false);
//       dgvInterviews.Refresh();
//       dgvDemarcationSets.Refresh();
//       dgvCodingSets.Refresh();
//       this.Refresh();
}	
The log entry from theWriteLog line confirms that this event handler is getting called. According to the VS2008 documentation, calling ResetBindings on the BindingSource should automatically refresh all the controls which use that BindingSource as the DataSource. The commented lines weresuggestions from other posts,which did nothing.

Any suggestions?
Thanks.
Dan
Dan Hurwitz
dhurwitz  Tuesday, September 15, 2009 1:52 PM

Thanks for the reply. Based on further thoughts of the problem overnight, reinforced by the questions you asked, I solved the problem. Your question about bsDemarcationSets seeing the new row was the key.

The datasource's for the BindingSource's were DataTables from a DataSet. So I factored that code out into a method called BindGrids. Then I called BindGrids both when the grids are initially created, and also when the button event is fired. Works like a charm.

Thanks.

Dan


Dan Hurwitz
  • Marked As Answer bydhurwitz Wednesday, September 16, 2009 1:11 PM
  •  
dhurwitz  Wednesday, September 16, 2009 1:11 PM

Hi Dan Hurwitz,

> A button on the form (actually a button contained in a user control on the form) fires off a database update which adds a record to the middle DataGridView, dgvDemarcationSets.

Based on my understanding, the update action will insert row into the table in database that related to bsDemarcationSets’s datasource. So after you click the “ucButtons� the datasource of bsDemarcationSets ought to have newly added row.

Could you please tell me what kind of DataSource do you use? If you use DataTable/DataSet, you can put a break point at “bsDemarcationSets.ResetBindings(false);�to take a quick watch of the datatable/dataset in bsDemarcationSets.DataSource. Does it contain the newly added row? If it doesn’t, you’d better show us the code of adding new row. That will help to locate the problem.

Please inform me your test result. If you have anything unclear, please tell me.

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Wednesday, September 16, 2009 8:36 AM

Thanks for the reply. Based on further thoughts of the problem overnight, reinforced by the questions you asked, I solved the problem. Your question about bsDemarcationSets seeing the new row was the key.

The datasource's for the BindingSource's were DataTables from a DataSet. So I factored that code out into a method called BindGrids. Then I called BindGrids both when the grids are initially created, and also when the button event is fired. Works like a charm.

Thanks.

Dan


Dan Hurwitz
  • Marked As Answer bydhurwitz Wednesday, September 16, 2009 1:11 PM
  •  
dhurwitz  Wednesday, September 16, 2009 1:11 PM

You can use google to search for other answers

Custom Search

More Threads

• Index was out of range. Must be non-negative and less than the size of the collection.
• Help understanding NotifyIcon
• Generic error in GDI+ when drawing in form OnPaint method.
• passing variables to NotifyIcon Text
• Can someone tell me how to add a simple white space using C# in string... forget the code...
• Chaning fonts size over multiple fonts in rtfbox
• Which of the following things is considered a best practice with win forms?
• How to detect bound DataGridView "cancel edit" at datatable level ?
• FilesListBox control articale
• How I can send one windows form resualt to another form?