When you use a BindingSource, for example by dragging it on to a page to bind data to a DataGrid or BindingNavigator, you will eventually need to cast the object that is current in the BindingSource into a type you can use to read or edit it. Typically code as shown below is used to cast the BindingSource's Current object to a strongly typed DataRow:
Dim currentCustomer As DataService.AjaxDataSet.CustomerRow
currentCustomer = CType(CType(Me.AjaxCustomerBindingSource.Current, DataRowView).Row, DataService.AjaxDataSet.CustomerRow)
To assist you with casts like this, Visual Basic 2005 provides an Intellisense code snippet you can invoke to insert a template for the cast into the code edit, that you can then navigate through and 'fill in the blanks'. Visual Basic 2005 IntelliSense Code Snippets are reusable, task-oriented blocks of code. Visual Studio 2005 includes code snippets covering tasks ranging from creating a custom exception, to sending an e-mail message, to drawing a circle.
At a point where you would normally add the casting code manually instead type: dtBindingSourceCurrent and then type the Tab key. The result in the code editor will be:
Dim row As NorthwindDataSet.CustomersRow
row = CType(CType(Me.CustomersBindingSource.Current, DataRowView).Row, NorthwindDataSet.CustomersRow)
Now you can tab through the green editing areas changing the templates default 'NorthwindDataSet.CustomersRow' to your data table, and default 'CustomerBindingSource' to your binding source.
You will only have to replace the first 'NorthwindDataSet.CustomersRow' in the template, the second 'NorthwindDataSet.CustomersRow' will be replaced automatically when you replace the first.
As an alternative to typing 'dtBindingSourceCurrent' into the code editor to insert the code snippet you can also right-click the code editor and select 'Insert Snippet...'. An 'Insert Snippet' dialog will appear in the code. In the drop down menu select:
Insert Snippet -> Data – Design-time features and ADO.net -> Insert Snippet -> Converts BindingSource.Current to a specific row in a DataTable
mike mcintyre http://www.getdotnetcode.com
Subscribe to GetDotNetCode's How To News Feed