Can you please point me to an example that uses linq data as a source for the grid in the scenario something like below:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource =
this.GridSource2;
}
private object GridSource2
{
get
{
Object obj = this.ViewState["_gds"];
if (obj != null)
{
return obj;
}
else
{
string s;
object ss = from i in EntityConnection.INVENTORY select new { i.INV_DEVICENAME, i.INV_DEVICETYPE };
this.ViewState["_gds"] = ss;
return ss;
}
}
}
6 Answers, 1 is accepted
For an example how to use manual populate RadGrid control using LINQ context please refer to this online demo. Although LinqToSql context is used in the particular demo, same as demonstrated approach applies for EF too.
Best wishes,
Rosen
the Telerik team
************** #1 ***********************
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };
RadGrid1.MasterTableView.DataKeyNames =
new string[] { "INV_IRN" };
}
****************************************
**************** #2 *********************
private object GridSource2
{
get
{
Object obj = this.ViewState["_gds"];
if (obj != null)
{
return obj;
}
else
{
object ss = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };
this.ViewState["_gds"] = ss;
return ss;
}
}
}
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource =
this.GridSource2;
RadGrid1.MasterTableView.DataKeyNames =
new string[] { "INV_IRN" };
}
****************************************
Maybe I need to explain a little more about my post above. I can connect to the grid to a linq query just fine directly in the NeedDatasource event like this:
RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };
But when I use the same query in a separate routine that uses the 'ViewState' object as per the demos I get a 'serialisation' error. Please help asap as my grid is currently unusable
I suspect that error you are facing is due to the fact that IQueryable<> returned from the entity context fails serialization. Therefore you should instead force query's execution first and then store the results. The easiest way to achieve this is by calling ToList() on the query.
Regards,
Rosen
the Telerik team
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new testclass { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME};
RadGrid1.MasterTableView.DataKeyNames = new string[] { "INV_IRN" };
}