Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom object in VB.NET- how to clear DataSource?
 

Custom object in VB.NET- how to clear DataSource?

Hi all-

I have a basic custom object (class), representing data in a binary file,whose properties are bound to controls on a Windows Form via Drag/Drop databinding. When the app runs, and the user clicks the "Open file" button and selects a file, I first instantiate the object without any data in its properties, then call a method in the object which reads the data intothe object's properties;then, I bind the instance of the object tothe BindingSource control's DataSource propertyin code.

All of the above works fine. But I also have a "Close file" button on the form, whose Code Behindneeds to clear the bound controls on the form, and clear the object. This is so the user can then select another file to load. My problem is, how to clear the BindingSource/object? When I simply set the BindingSource control's DataSourceproperty to Nothing (null), before I can set the object to Nothing, I get an exception: "Cannot bind to the property or column MyPropertyon the DataSource. Parameter name: dataMember", where "MyProperty" is the name of one of the object's properties.

Should I simply Catch the exception and swallow it? That doesn't seem like a good idea. Is there another way to approach this? I don't want to have to manually clear each control's Binding settings every time the user opens a file!

Thanks,

-Andrew

ACushen  Wednesday, July 29, 2009 3:14 AM

You can use the type instead of NULL:

bindingSource.DataSource = typeof(FileObject);

or maybe if the Close Buttons cleans all properties of the currrent object bindingSource.ResetBindings(false) is enough.

EDIT: This is C# code, I'm not exactly sure how that type stuff works in VB ...

--
Wolfgang

  • Marked As Answer byACushen Wednesday, July 29, 2009 3:43 PM
  •  
Wollinet  Wednesday, July 29, 2009 6:38 AM

You can use the type instead of NULL:

bindingSource.DataSource = typeof(FileObject);

or maybe if the Close Buttons cleans all properties of the currrent object bindingSource.ResetBindings(false) is enough.

EDIT: This is C# code, I'm not exactly sure how that type stuff works in VB ...

--
Wolfgang

  • Marked As Answer byACushen Wednesday, July 29, 2009 3:43 PM
  •  
Wollinet  Wednesday, July 29, 2009 6:38 AM
THANK YOU Wolfgang.

Your first idea appears to be working.

For the record, the syntax in VB.NET is:

MyObjectBindingSource.DataSource = GetType(MyObject)

where "MyObject" is the name of the custom object. TypeOf has a different function in VB.NET.


Thanks again,

-Andrew
  • Edited byACushen Wednesday, July 29, 2009 2:53 PM
  •  
ACushen  Wednesday, July 29, 2009 2:51 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridViewTextBoxColumn, override Paint problem
• System.Data.OleDb.OleDbException while querying access 2000
• working with MySQL data bases from windows ODBC connection
• Add data to a datagrid view
• Creating Buttons at Runtime from Data?
• Mixing bound and unbound columns
• Cannot Set properties for controls that are not visible
• Escape from DataGridViewComboBoxColumn = 3 blank rows
• How Do I Display a "Name" Using a "NameID" in a DatagridTextboxColumn
• Database Locking Error