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

[Solved] Problems binding Grid to List of EntityObject-objects (Entity Framework)

1 Answer 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Johan Bohlin
Top achievements
Rank 1
Johan Bohlin asked on 06 Nov 2009, 12:12 PM

I use problems binding RadGrid to Entity Framework-objects.

I have two tables:

User:
UserID
Name
Age
GroupID

Group:
GroupID
Name

In entity framework, i get two Entitytypes. First User with properties UserID, Name, Age and Navigation property Group. Entity set name is set to Users. Second EntityType is Group with GroupID and Name as properties, and a navigation property named Users pointing to a collection of User-entities.

To fetch all users and include the group i use the following code:

private List<User> GetUsers()  
{  
 var users = from u in entities.Users  
   .include("Group")  
   select u;  
 return users.ToList();  
    

Binding the grid to the list of User-objects is no problem.

Grid.Datasource = GetUsers();  
Grid.Databind();  
 
 

I add the following columns to the grid.

<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name" />  
<telerik:GridBoundColumn DataField="Age" UniqueName="Age" HeaderText="Age" />  
<telerik:GridBoundColumn DataField="Group.Name" UniqueName="Group" HeaderText="Group Name" />  
 
 

All data is displayed, no problem yet.

I want the 3rd column to be a GridDropDownColumn instead. I tried to use a GridDropDownColumn, but it didn't work, all rows were blank:

<telerik:GridDropDownColumn DataField="Group.Name" UniqueName="Group" HeaderText="Group" />  
 
 

I read in another thread that GridDropDownColumn is mainly for DataTables, so i tries to use GridTemplateColumn instead, and it almost works.

 

<telerik:GridTemplateColumn DataField="Group.Name" UniqueName="Group" HeaderText="Group">  
 <ItemTemplate>  
  <asp:Label ID="Label1" runat="server" Text='<%#Bind("Group.Name") %>'></asp:Label>  
 </ItemTemplate>  
 <EditItemTemplate>  
  <telerik:RadComboBox ID="EditGroupRadComboBox"  runat="server" />  
 </EditItemTemplate>  
</telerik:GridTemplateColumn>  
 

The RadComboBox is bound to data in ItemDataBound:

if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)  
{  
 GridEditableItem editedItem = e.Item as GridEditableItem;  
 RadComboBox combo = (RadComboBox)editedItem.FindControl("EditGroupRadComboBox");  
 
 combo.DataSource = GetGroups(); //returns List<Group>  
 combo.DataTextField = "Name";  
 combo.DataValueField = "GroupID";  
 
 combo.DataBind();  
 combo.SelectedValue = ((User)editedItem.DataItem).Group.GroupID.ToString();   
}  
 
 

It works as expected, the groups are displayed in the RadComboBox, and the correct option is selected.

Now to the problem, how do I perform the update? I tried he following in ItemCommand-event, but the user-object contains the old values

if (e.CommandName == "Update")  
{  
 User user = e.Item.DataItem as User;  
}  
 

I looked at the following thread, with a similar problem: http://www.telerik.com/community/forums/aspnet-ajax/grid/radcombobox-in-gridtemplatecolumn.aspx
I tried to set SelectedValue='<%#Bind("Group.GroupID") %>', but i get an exception, saying Group.GroupID is invalid.

 
Kind Regards, Johan

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 11 Nov 2009, 03:37 PM
Hello Johan,

Thank you for the detailed explanation.

Indeed binding to sub-objects with dot syntax is not supported for built-in GridDropDownColumn. You can use RadComboBox inside edit template of GridTemplateColumn as you already found out. To extract the new value from the combobox editor on update or insert operation, consider the options presented on the online demos of the product below:

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Johan Bohlin
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or