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

Grid - Entity framework

3 Answers 134 Views
Grid
This is a migrated thread and some comments may be shown as answers.
George
Top achievements
Rank 1
George asked on 12 Dec 2011, 04:27 PM
Hi,

I am new to Entity framework 4.0 combine with telerik and I want your help please. I have a grid which is bind to Users entity. All users are assigned to a role (administrator,manager....e.t.c). The relation is many to one. On the grid I manage to displayed the users with all the necessary information including the roles. The user role is displayed inside the grid having a radcombobox and using a separate entitydatasource to fetch all the availble roles exists in database.
- My first problem is that the "role" comboBox doesn't select the correct value each user has during grid load(the first item is always selected).
- Second main problem is that if I want to update a record no changes are applied to the selected record(nothing happens).

Sample code below:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True"
        AllowPaging="True" AllowSorting="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
            AllowAutomaticDeletes="True" AutoGenerateDeleteColumn="True"
        AutoGenerateEditColumn="True" DataSourceID="EntityDataSource1">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID"
                DataSourceID="EntityDataSource1" CommandItemDisplay="Top">
    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </RowIndicatorColumn>
    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="NAME"
                FilterControlAltText="Filter NAME column" HeaderText="NAME"
                SortExpression="NAME" UniqueName="NAME">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SURNAME"
                FilterControlAltText="Filter SURNAME column" HeaderText="SURNAME"
                SortExpression="SURNAME" UniqueName="SURNAME">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="ROLE_ID" DataType="System.Int32"
                FilterControlAltText="Filter ROLE_ID column" HeaderText="ROLE_ID"
                SortExpression="ROLE_ID" UniqueName="ROLE_ID">
                <EditItemTemplate>
                    <asp:EntityDataSource ID="roleDS" runat="server"
                        ConnectionString="name=TestDbEntities" DefaultContainerName="TESTDbEntities"
                        EnableFlattening="False" EntitySetName="ROLE" Select="it.[ID], it.[NAME]">
                    </asp:EntityDataSource>
                    <telerik:RadComboBox ID="ROLE_IDComboBox"   runat="server" DataSourceID="RoleDS" DataTextField="NAME" DataValueField="ID">
                    </telerik:RadComboBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="ROLE_IDLabel" runat="server" Text='<%# Eval("ROLE_ID") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    <EditFormSettings>
    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
    </EditFormSettings>
    </MasterTableView>
    <FilterMenu EnableImageSprites="False"></FilterMenu>
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
</telerik:RadGrid>
     
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=TestDbEntities" DefaultContainerName="TESTDbEntities"
EnableDelete="True" EnableFlattening="False" EnableInsert="True"
EnableUpdate="True" EntitySetName="USER">
</asp:EntityDataSource>


Can you provide me an example using entity framework with grid having many to one relation records. I can't find any examples.

Thanks in advance

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Dec 2011, 12:13 AM
Hello George,

For your convenience I created a simple demo showing how to implement the desired functionality. Please give it a try and let me know if you need further assistance.

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
Nick
Top achievements
Rank 1
answered on 10 May 2012, 09:11 PM
Hi,

I'm new to the Telerik controls and I'm using Entity Framework. I'm wondering how I can use my insert stored procedure for the insert command of my rad grid? I've seen documentation on how to do manual CRUD via SQLDataSource, but I'm looking for a way to avoid that and use my EF procedure instead.

Thanks,
Nick
0
Daniel
Telerik team
answered on 15 May 2012, 09:45 AM
Hello Nick,

You should handle the corresponding RadGrid command (InsertCommand) and pass the new values to your insert procedures in the same way it is done in the following demo:
Entity Framework Manual Operations

Dummy code:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem item = e.Item as GridEditableItem;
    Hashtable values = new Hashtable();
    item.ExtractValues(values);
    MyObject obj = new MyObject();
    item.UpdateValues(obj);
    MyContext.MyInsertMethod(obj);
    MyContext.SaveChanges();
}

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.
Tags
Grid
Asked by
George
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Nick
Top achievements
Rank 1
Share this question
or