This is a migrated thread and some comments may be shown as answers.

RadGrid and Entity Framework in Code-behind

8 Answers 424 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gonzalo
Top achievements
Rank 2
Gonzalo asked on 07 Sep 2012, 06:20 PM
Hi dear friends!

Do you have a sample of asp.net webforms page populating a radgrid with entity framework objects in code behind. Example:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object senderEventArgs e)
        {
            ClientsContext db = new ClientsContext();
            var query = from b in db.Clients orderby b.LastName select new { Client = b.ClientIdFirst = b.FirstNameLast = b.LastNameCounty = b.CountyName, City = b.City };
            GridView1.DataSource = query.ToList().Take(100);
        }
    }
}

Your support will be greatly appreciated.

G

8 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 12 Sep 2012, 02:51 PM
Hello Gonzalo,

I believe the following demo will give you a good starting point:

Best regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Gonzalo
Top achievements
Rank 2
answered on 12 Sep 2012, 03:57 PM
Thanks Daniel. I will check this out.
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 06 Feb 2015, 10:07 AM
Hi Daniel,

I believe there is an error in the C# code listed in your link, it shows:

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    var result = from r in DbContext.Products
                         select r;
    RadGrid1.DataSource = result;
}

But that gives errors, should it not be:
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    var result = from r in DbContext.Products
                         select r;
 
    RadGrid1.DataSource = result.toList();
 
}




​
0
Kostadin
Telerik team
answered on 11 Feb 2015, 07:59 AM
Hi Al,

Know that this is the exact code which is used for creating the demo. I am not sure why the code throws an error on your side without replicating the issue locally. Could you please provide your an image of the exception that you have received and also your code declaration?

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 11 Feb 2015, 08:33 AM
Hi Kostadin,

Please see the attached pic and code below.

<telerik:RadGrid ID="grdMain" runat="server" AllowFilteringByColumn="True" AllowSorting="True"
    AutoGenerateDeleteColumn="false" AutoGenerateEditColumn="false"
    ShowGroupPanel="True" OnNeedDataSource="grdMain_NeedDataSource" OnPreRender="grdMain_PreRender" OnItemDataBound="grdMain_ItemDataBound"
    OnDeleteCommand="grdMain_DeleteCommand" AutoGenerateColumns="true" >
    <ClientSettings AllowDragToGroup="True">
        <ClientEvents OnCommand="grdMain_OnCommand" />
    </ClientSettings>
    <MasterTableView CommandItemDisplay="Top" EditMode="PopUp" >
        <RowIndicatorColumn Visible="False">
        </RowIndicatorColumn>
        <ExpandCollapseColumn Created="True">
        </ExpandCollapseColumn>
        <Columns>                       
            <telerik:GridButtonColumn CommandName="Edit" UniqueName="Edit" Text="Edit" ItemStyle-Width="30px"></telerik:GridButtonColumn>
            <telerik:GridButtonColumn CommandName="Delete" UniqueName="Delete" Text="Delete" ConfirmDialogType="RadWindow" ConfirmText="Continue to delete?" ItemStyle-Width="30px"></telerik:GridButtonColumn>                       
            <telerik:GridButtonColumn CommandName="Groups" UniqueName="Groups" Text="Groups" ItemStyle-Width="30px"></telerik:GridButtonColumn>
            <telerik:GridButtonColumn CommandName="Rights" UniqueName="Rights" Text="Rights" ItemStyle-Width="30px"></telerik:GridButtonColumn>
            <telerik:GridButtonColumn CommandName="AD_Paths" UniqueName="AD_Paths" Text="AD_Paths" ItemStyle-Width="60px"></telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

​
0
Kostadin
Telerik team
answered on 16 Feb 2015, 07:57 AM
Hello Al,

Could you please provide your code behind since I am still not able to replicate the issue that you are facing? Additionally which version of RadControls you are using?

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 16 Feb 2015, 08:03 AM
Hi Kostadin,

I did post my code-behind previously, nothing fancy there. In NeedDataSource I am using :

var result = from r in DbContext.Products
                     select r;
RadGrid1.DataSource = result.ToList();

If I don't use add ToList() then I get the error. I am using version  2014.3.1209.45
0
Kostadin
Telerik team
answered on 19 Feb 2015, 06:28 AM
Hello Al,

I isolate the example from the demos in a small runnable sample. As you can see there is no need to call ToList of the result. Could you please verify that you are using exactly the same entity model? If you are using a different one you might return a different type of data that requires to call toList as the exception notes.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Gonzalo
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Gonzalo
Top achievements
Rank 2
Al
Top achievements
Rank 1
Iron
Iron
Iron
Kostadin
Telerik team
Share this question
or