My Grid:
<dg:DataGrid Grid.Column="0" Grid.Row="0" Name="recipe_grid" ItemsSource="{Binding}" AutoGenerateColumns="False"
IsReadOnly="True" HeadersVisibility="Column" FontSize="14" FontFamily="Arial Narrow" FontWeight="Bold"
Background="Transparent" RowBackground="Transparent" BorderBrush="DarkGray"
CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False" ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" BorderThickness="0" VerticalGridLinesBrush="#FF6C6464" HorizontalGridLinesBrush="#016C6464">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding Path=name}" Header="name"/>
<dg:DataGridTemplateColumn Header="country" CanUserReorder="False" CanUserSort="False" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource CountryListData}}" DisplayMemberPath="Name" SelectedValue="idcountry" Name="combo_country"></ComboBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
Resources:
<local:CountryList x:Key="CountryListData"/>
Code: Main:
DataTable car = new DataTable();
car.Columns.Add(new DataColumn("id_car", typeof(int)));
car.Columns.Add(new DataColumn("name", typeof(string)));
car.Columns.Add(new DataColumn("id_country", typeof(int)));
Country:
public class CountryList : ObservableCollection<CountryName>
{
public CountryList(): base()
{
NpgsqlConnection conn = new NpgsqlConnection("Server=localhost; Port=5432; UserId=postgres; Password=whatever; Database=backup");
conn.Open();
DataSet CL = new DataSet();
DataTable country = new DataTable();
country.Columns.Add(new DataColumn("id_country", typeof(int)));
country.Columns.Add(new DataColumn("name", typeof(string)));
CL.Tables.Add(country);
NpgsqlDataAdapter country_fill = new NpgsqlDataAdapter();
country_fill = new NpgsqlDataAdapter();
country_fill.SelectCommand = new NpgsqlCommand();
country_fill.SelectCommand.Connection = conn;
country_fill.SelectCommand.CommandText = "SELECT * FROM country";
country_fill.Fill(country);
conn.Close();
foreach (DataRow row in country.Rows)
{
Add(new CountryName((string)row.ItemArray[1], (int)row.ItemArray[0]));
}
}
}
public class CountryName : INotifyPropertyChanged
{
public string name;
public int id_country;
public event PropertyChangedEventHandler PropertyChanged;
public CountryName(string country_name, int id)
{
this.name = country_name;
this.id_country = id;
}
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("CountryName");
}
}
public int idcountry
{
get { return id_country; }
set { id_country = value; }
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
I didn't post all of the code but you can get the idea. I need to display the name of the Country instead of id_country in a DataGrid filled with cars. As you can see at this point all I have is a DataTable binded to a DataGrid that shows all of the cars and an ObservableCollection that contains the countries. In WinForms everything was easy as pie. But now I've gotten only this far(with great help of this community). How can I make the ComboBox display only one value that would be the correct name of the country for each car?