[Mono-dev] PagedDataSource and DataTable (IListSource) DataSource

Matt Petteys mpetteys at yellowguppy.com
Sun Dec 4 12:16:49 EST 2005


I have a datagrid in an form which has the DataSource set with a 
System.Data.DataTable.  On the MS runtime the datagrid, successfully 
databinds and displays the data.

On Mono, it fails with the error..

<0x00175> System.Web.UI.WebControls.PagedDataSource:get_DataSourceCount 
() in <0x00034> System.Web.UI.WebControls.PagedDataSource:get_PageCount 
() in <0x000ce> 
System.Web.UI.WebControls.DataGrid:InitializeNumericPager 
(System.Web.UI.WebControls.DataGridItem item, Int32 columnSpan, 
System.Web.UI.WebControls.PagedDataSource paged) in <0x00051> 
System.Web.UI.WebControls.DataGrid:InitializePager 
(System.Web.UI.WebControls.DataGridItem item, Int32 columnSpan, 
System.Web.UI.WebControls.PagedDataSource pagedDataSource) in <0x000a7> 
System.Web.UI.WebControls.DataGrid:CreateItem (Int32 item_index, Int32 
data_source_index, ListItemType type, Boolean data_bind, System.Object 
data_item, System.Web.UI.WebControls.PagedDataSource paged) in <0x00273> 
System.Web.UI.WebControls.DataGrid:CreateControlHierarchy (Boolean 
useDataSource) in <0x00095> 
System.Web.UI.WebControls.BaseDataList:DataBind ()

The datagrid definition is..

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" 
PageSize="20" AllowPaging="True" CssClass="datatable">
...
<PagerStyle Mode="NumericPages">
</PagerStyle>
</asp:DataGrid>

The error generated on the webform is..

*Error Message: *HTTP 500. The data source must implement ICollection

This error appears to maps back to 
http://svn.myrealbox.com/source/trunk/mcs/class/System.Web/System.Web.UI.WebControls/PagedDataSource.cs

public int DataSourceCount {
get {
if (source == null)
 return 0;
				
if (IsCustomPagingEnabled)
 return virtual_count;

if (source is ICollection)
 return ((ICollection) source).Count;

throw new HttpException ("The data source must implement ICollection");
}
}

Since a DataTable doesn't implement ICollection directely it appears to 
be causing the problem?  Should something like the following be added to 
the DataSourceCount method before the error is thrown?

if (source is IListSource) {
 return ((ICollection) ((IListSource)source).GetList() ).Count;
}

Additionally, the GetEnumerator method doesn't appear to anticipate a DataTable or IListSource object.  Something like the following might be appropriate.

public IEnumerator GetEnumerator ()
{

// IList goes first, as it implements ICollection
> IList list
> if (source is IListSource) {
>  list = ((IListSource) source).GetList();
> else {
>   list = source as IList;
> }

int first = 0;
int count;
int limit;
if (list != null) {
first = FirstIndexInPage;
count = ((ICollection) source).Count;
limit = ((first + page_size) > count) ? (count - first) : page_size;
return GetListEnum (list, first, first + limit);
}

ICollection col = source as ICollection;
if (col != null) {
first = FirstIndexInPage;
count = col.Count;
limit = ((first + page_size) > count) ? (count - first) : page_size;
return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
}

return source.GetEnumerator ();
}

If this makes sense then I can submit the appropriate bug.

Matt Petteys




More information about the Mono-devel-list mailing list