views:

101

answers:

1

Hi


If ObjectDataSource.SelectMethod = A and if A() returns a single object of type O, then this object is wrapped in a one element IEnumerable collection and returned by the ODS.Select method

Thus, assuming ODS.SelectMethod points to method A(), then if A() returns a null , when called by ODS, this null is ( I assume ) wrapped into one element IEnumerable collection and no exception is thrown. Similarly, if ODS.SelectMethod points to method B(), which returns a list of objects, and if B() returns an empty collection when called by ODS, then no exception is thrown.

But if a method would instead return ( when called by ODS.Select ) an empty string collection (string[]), then I’d get exception “The data source for GridView did not have any properties or attributes from which to generate columns”. How is empty string collection any different from other empty collections, which don’t cause an exception?


Thanx

+2  A: 

If your GridView has AutoGenerateColumns set to true, it requires a sequence of objects which have bindable members, like a DataRow. The column autogenerator inspects the signature of the sequence passed in and knows how to handle a few various cases, such as a DataRow which has a collection of columns which can be inferred into a list of columns on the GridView control. A string has no such properties. Set AutoGenerateColumns to false, and define your own column, like so:

<Columns>
    <asp:TemplateField>
        <ItemTemplate><%# Container.DataItem %></ItemTemplate>
    </asp:TemplateField>
</Columns>

A string is pretty limited as a datasource for a GridView - unless you plan to eval properties about the string like it's Length, you can really just print the string itself (DataItem).

Rex M
I don't quite understand how all that relates to the fact that when string[] doesn't contain any string objects, an exception is thrown, but if a List<> is empty (thus doesn't contain any objects to which gridView's rows could be data bound ), then no exception is thrown?
carewithl
@Care it still tries to generate the columns, just no rows.
Rex M