0

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?

boo_boo_bear
  • 189
  • 1
  • 4
  • 14
  • Is the name of the countries not getting displayed in the Combobox? Your DisplayMemberPath correctly points to the Name Property of the CountryName object and hence you should see the combobox populated with the name. For selecting the correct value in the combobox use SelectedValuePath="idcountry" instead of SelectedValue="idcountry". Your question doesn't clearly identify the issue that you are facing. Is your Datagrid binding an issue or the problem is only with Combobox inside the Datagrid? – Jatin Jun 01 '13 at 11:07
  • It displays the names, the problem is that I want it to display only the country name the car is from. I have not set the Relation between those two tables, because I don't know how to do it if the tables come from different classes and different DataSets. – boo_boo_bear Jun 01 '13 at 11:20
  • Oh I see, then you really don't need a combobox for that. I think you are trying to set up relation between the 2 datatables based on country_id to get the country name – Jatin Jun 01 '13 at 11:42
  • 1
    Right now the simple solution is to add an extra field in the Car Datatable which holds the Country Name. You can fill this field by looking up the CountryName ObservalbeCollection based on the id_country value. Then bind this field to the column as you would for a normal DataGridTextColumn. If you wan't to persist with a Combobox, then you will have to manually filter entries of Combobox and there seems to be no easy way. – Jatin Jun 01 '13 at 11:59
  • Thank you that will solve my problem in this particular DataGrid, but the thing is in another part of the app I have a DataGrid with the same car table that I want to add CRUD functions to, so at one point I will need to bind them anyway. Actually the thing is I was trying to think of a way to filter the Combobox entries, before I posted here, but I couldn't come up with how to do it. – boo_boo_bear Jun 01 '13 at 12:21
  • Then you would want to use a combobox for the Country Name column. And since it has a CRUD feature, I assume that the Combobox will contain a list of all countries, for user to select one. Then in that case you can simply use the **DisplayMemberPath**, **SelectedValue** and **SelectedValuePath** together to populate Cars Itemsource with user selected values. I had great problems understanding these when I began using wpf. Hopefully, this post should clarify them http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath – Jatin Jun 01 '13 at 12:30
  • Bassically I would just get the SelectedValue from the ComboBox and pass it as the id_country to Cars, right? – boo_boo_bear Jun 01 '13 at 12:55
  • Exactly. And the **SelectedValuePath** chooses which property to pick as SelectedValue from the CountryName object (idcountry in your case). – Jatin Jun 01 '13 at 12:58
  • Thanks for explaining. Now everything seems clear to me. And I'm kinda happy to hear that someone else has had problems of getting the grasp of this. For a moment I was starting to feel really blonde. – boo_boo_bear Jun 01 '13 at 13:12
  • there is one little problem left. I was trying to do it on the ComboBoxes SelectionChanged event, but in the code behind it doesn't recognize my ComboBoxes name. Or am I still doing it wrong and I had to just bind the SelectedValue with the id_country columns SelectedValue in XAML somehow? – boo_boo_bear Jun 01 '13 at 13:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31035/discussion-between-nirvan-and-boo-boo-bear) – Jatin Jun 01 '13 at 13:27

0 Answers0