Introduction
For those working with data sources, you might not know whether the source is a DataTable or a DataView. Here is a library of common FarPoint utilities for code concurrency and quicker future maintenance that helps determine whether a DataSource is a DataTable or DataView object. These two functions allow you to use either one depending upon your needs without repeating code (such as If TypeOf DataSource Is DataTable, etc.) everywhere. FarPoint has a GetChildRelation which returns the DataRelation.RelationName but not the DataRelation itself. Where this is useful is, for example, in a DataSet’s DataRelation collection, which can be referenced via RelationName. The GetDataView and GetDataTable are overloaded to accept a SheetView, FpSpread, or DataSource input parameter. If FpSpread is the input parameter, it uses active sheet. An exception is thrown if the DataSource is neither a DataView nor a DataTable object.
Though they can be a pain to set up initially, I am a big fan of overloads rather than multiple, optional parameters because they then automatically generate the little combo-box lists in IntelliSense. I try to write all of the overloads so that they end up pointing to either one main one where all of the code resides or to a private version. The method where the code resides is listed first, followed by the various method overloads.
The functions were written to accept an FpSpread object, SheetView object, or DataSource object as input parameters. An FpSpread object uses the ActiveSheet.DataSource and a DataView object uses DataView.DataSource. By using FpSpread you are always assured of getting the value for the currently active sheet. I am a proponent of enhancing generalization! If you have any comments about this article feel free to drop me a line at j2associates@yahoo.com. I hope you find these overloads helpful.
Code
DataTable code
''' -----------------------------------------------------------------------------
''' <summary>
''' </summary>
''' <param name="dataSource"></param>
''' <returns>DataTable property for given DataSource object - Throws ArgumentException if DataSource is neither DataTable nor DataView</returns>
''' <remarks>Dim dt As DataTable = GetDataTable(SheetView.DataSource)
''' </remarks>
''' -----------------------------------------------------------------------------
Public Shared Function GetDataTable(ByVal dataSource As Object) As DataTable
If TypeOf dataSource Is DataTable Then
Return CType(dataSource, DataTable)
ElseIf TypeOf dataSource Is DataView Then
Return CType(dataSource, DataView).Table
Else
Throw New ArgumentException("Unknown DataSource '" & dataSource.GetType.Name & "'", "dataSource")
End If
End Function 'GetDataTable
''' -----------------------------------------------------------------------------