DataViewManager, as part of its ITypedList implementation, exposes the DataTables as properties
in its DataViewSettingCollection. The code checks the dynamic properties of DataViewManager
to see if it can retrieve the DataViewSetting property that matches the DataMember passed into
the Repeater control. If the DataMember is blank, we choose the first DataTable in the DataSet.
Listing 7-3 presents the full source code for the DataSourceHelper class.
Listing 7-3. The DataSourceHelper Class File
using System;
using System.Collections;
using System.ComponentModel;
namespace ControlsBook2Lib.Ch07
{
public class DataSourceHelper
{
public static object ResolveDataSource(object dataSource, string dataMember)
{
if (dataSource == null)
return null;
if (dataSource is IEnumerable)
{
return (IEnumerable)dataSource;
}
else if (dataSource is IListSource)
{
IList list = null;
IListSource listSource = (IListSource)dataSource;
list = listSource.GetList();
if (listSource.ContainsListCollection)
{
ITypedList typedList = (ITypedList)list;
PropertyDescriptorCollection propDescCol =
typedList.GetItemProperties(new PropertyDescriptor[0]);
if (propDescCol.Count == 0)
throw new Exception("ListSource without DataMembers");
PropertyDescriptor propDesc = null;
//Check to see if dataMember has a value, if not, default to first
//property (DataTable) in the property collection (DataTableCollection)
if ((dataMember == null) || (dataMember.
Pages:
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412