I am developing a a Sharepoint 2010 visual webpart and I am trying to use LinqDataSource in it to handle paging and sorting in a GridView. I made my datacontext and entity objects with spmetal. and now this is my code:
my mark up:
<%@ Register TagPrefix="asp" Namespace="System.Web.UI.WebControls" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<asp:LinqDataSource runat="server" ID="LinqDataSource1" OnSelecting="MySelecting" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3"
AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
</asp:GridView>
and my code:
protected void MySelecting(object sender, LinqDataSourceSelectEventArgs e)
{
TestEntitiesDataContext dc = new TestEntitiesDataContext("http://sp/sites/test");
e.Result = from item in dc.TestList
select new
{
title = item.Title,
numberField = item.NumberField.ToString()
};
}
Now the problem is when I try to view the webpart on the site I get this error: Expression of type 'System.Int32' cannot be used for return type 'System.Object'
When I deactivate paging on grid view this error disappears.
Do you have any idea why this is happening?
I 'd be grateful of any help.
for me the following work around has helped:
in MySelecting:
And if you need sorting, you should also process e.Arguments.SortExpression.