Is there a way to bind to lookup values in a gridview?
What I'm trying to achieve is nothing more complex than in Windows Forms ValueMember and DisplayMember:
This code is from the RadControls for WinForms example:Column Types
//add new lookup column |
GridViewLookUpColumn lookUpColumn = new GridViewLookUpColumn(); |
lookUpColumn.HeaderText = "Look up"; |
lookUpColumn.FieldName = "PersonID"; |
lookUpColumn.DataSource = this.employeesBindingSource; |
lookUpColumn.ValueMember = "EmployeeID"; |
lookUpColumn.DisplayMember = "LastName"; |
Given the classes below where the GridView binds to PriceList which is a collection of Price(s), I want the CommodityId column to be a combo box which, when in edit mode gives me a list of Commodity entities showing the Description text and when in view mode shows me the Description text, but saves the Commodity.Id value back to AppPresenter.PriceList.
By including DataMemberPath in the CommodityId field of the DataView, I am able to display Commodity.Description. However, the selected value from the combo is not reflected in the underlying datasource (PriceList).
If I remove DataMemberPath from the markup - it is clear to see that the PriceList is never updated with the newly selected value.
I'm guessing that the reason for this is that I am not binding Commodity.Id (in the lookup) to CommodityId in the GridView. So my question is - how do I do this binding? The example for DataBinding doesn't seem to cover this case.
public class Commodity() |
{ |
public int Id {get;set;} |
public string Description {get;set;} |
public override ToString() |
{ |
return Description; |
} |
} |
public class Price |
{ |
public int Id {get;set;} |
public DateTime Date {get;set;} |
public int CommodityId {get;set;} |
public EntityRef<Commodity> Commodities {get; set;} |
} |
public class AppPresenter |
{ |
public ObservableList<Price> PriceList {get;} |
} |
public class Shell |
{ |
pubic Shell() |
private IList<Commodity> _commodityList; |
{ |
InitializeComponents(); |
_commodityList = repository.GetCommodites(); |
colCommodityEditorSettings = new ComboBoxEditorSettings { ItemsSource = _commodityList}; |
colCommodity.EditorSettings = colCommodityEditorSettings; |
} |
} |
<telerik:RadGridView Grid.ColumnSpan="4" Grid.Row="3" Margin="5" x:Name="radGridView1" ColumnsWidthMode="Auto" ItemsSource="{Binding Path = PriceList}" |
AutoGenerateColumns="False"> |
<telerik:RadGridView.Columns> |
<telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="ID" UniqueName="Id" x:Name="colId" /> |
<telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Date" UniqueName="Date" IsAutoGenerated="False" IsVisible="True" x:Name="colDate" /> |
<telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Hidden" UniqueName="IsHidden" x:Name="colIsHidden" /> |
<telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Commodity" UniqueName="CommodityId" x:Name="colCommodity" DataMemberPath="Commodities.Description"> |
</telerik:RadGridView> |
Many thanks in advance for your help
Jeremy Holt