I have a window with 2 controls. One is a combobox (cmbUnit) and another is a DataGrid (dgvProducts)
cmbUnits is binded with List<Unit> collection & the Unit class is below
public class Unit
{
public int ID { get; set; }
public string Name { get; set; }
}
Note : Display member is Name & Value member is ID
Where as, dgvProducts is binded with List<Product> Collection & the Product class is below
public class Product
{
public int ID { get; set; }
public string ProductName { get; set; }
public int UnitID { get; set; }
}
Here, i am selecting UnitID from my db table (TBL_PRODUCTS), I can also retrieve UnitName by joining table. for now, i want to keep only UnitID in my Product class.
My question is, My datagrid has a column (datagridTemplateColumn) with a combobox as the child, and i want to show the UnitName inside it by passing UnitID same like my combobox control (cmbUnit)
My Datas inside datagrid should be
--------------------------------------------------
ID UnitName ProductName
--------------------------------------------------
12345 Kg Sample Product
Where Kg should be displayed inside a combobox.
How to achieve this ? Help me !
My XAML code for cmbUnit is
<ComboBox Grid.Column="7" Grid.Row="1" Name="cmbUnit" ></ComboBox>
where i bind datas to this control from code behind.
XAML for dgvProducts
<DataGrid AutoGenerateColumns="False" Grid.ColumnSpan="2"
Name="dgvProduct"
Grid.Row="2">
<DataGrid.Columns>
<DataGridTemplateColumn Header="ID" Width="50" CanUserSort="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label VerticalContentAlignment="Center" Content="{Binding ID}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Unit" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEnabled="False"
Name="cmbGrUnit"
ItemsSource="{Binding Units}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{Binding UnitID}"
></ComboBox>
<!--<Label VerticalContentAlignment="Center" Content="{Binding Name}"></Label>-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Product Name" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label VerticalContentAlignment="Center" Content="{Binding ProductName}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn></DataGrid.Columns>
Thanks