在DataGrid中绑定ComboBox

时间:2014-02-26 20:00:57

标签: wpf datagrid combobox code-behind

我无法弄明白如何在代码隐藏的DataGrid中绑定ComboBox:

我的数据网格:

    <DataGrid x:Name="mydataGridView" HorizontalAlignment="Left" Margin="22,5,0,0" Width="596" ItemsSource="{Binding}"
                  AutoGenerateColumns="False" Height="242" VerticalAlignment="Top">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding artikel_id}" Header="ArtikelID" Visibility="Hidden"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding sifra}" Header="Šifra" Width="60"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding naziv}" Header="Naziv" Width="250"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding em}" Header="EM" Width="30"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding znesek}" Header="Znesek" Width="80"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding ddv}" x:Name="DDV" Header="DDV" Width="50"></DataGridTextColumn>
            <DataGridTemplateColumn Header="EM">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox
                            Name="cmbEM"
                            ItemsSource="{Binding}"
                            Width="60"
                        />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

代码:

        try
        {
            ds = new DataSet();
            NpgsqlConnection conn = new NpgsqlConnection(strConnString);
            conn.Open();
            myAdapter = new NpgsqlDataAdapter("SELECT artikel_id, coalesce(skupina_artikla, -1) skupina_artikla, user_changed, date_changed, naziv, "
                                                            + " round(ddv*100, 1) ddv, znesek, em, sifra "
                                                            + " FROM artikel", conn);
            myAdapter.Fill(ds, "Artikli");                
            this.DataContext = ds.Tables["Artikli"].DefaultView;
            ComboBox myCmbEm = (mydataGridView.Columns[6] as DataGridTemplateColumn).CellTemplate.LoadContent() as ComboBox;
            NpgsqlDataAdapter myAdapter1 = new NpgsqlDataAdapter("select em, naziv from em", conn);
            DataSet ds1 = new DataSet();
            myAdapter1.Fill(ds1, "EM");
            myCmbEm.ItemsSource = ds1.Tables[0].DefaultView;
            myCmbEm.DisplayMemberPath = ds1.Tables[0].Columns["naziv"].ToString();
            myCmbEm.SelectedValuePath = ds1.Tables[0].Columns["em"].ToString();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            throw;
        }

在datagrid中,我看到除了ComboBox之外填充的所有列都是空的。我做错了什么?

- &GT;更新

我添加了DataGridComboBoxColumn:

            <DataGridComboBoxColumn x:Name="cmbTest1" Header="EM" DisplayMemberPath="naziv" SelectedValuePath="em"  SelectedValueBinding="{Binding em1}"></DataGridComboBoxColumn>
        </DataGrid.Columns>

在代码中绑定它:

            cmbTest1.ItemsSource = ds1.Tables[0].DefaultView;
            cmbTest1.DisplayMemberPath = ds1.Tables[0].Columns["naziv"].ToString();
            cmbTest1.SelectedValuePath = ds1.Tables[0].Columns["em"].ToString();

并且有效。

此致 伊戈尔

0 个答案:

没有答案