【问题标题】:DataGridComboBoxColumn show Foreign Key Table - WPFDataGridComboBoxColumn 显示外键表 - WPF
【发布时间】:2016-10-19 19:41:42
【问题描述】:

我有一个 SQL 数据库,其中包含表 Jars 和 JarTypes。我需要在 DataGrid 中显示数据。我还必须能够编辑行。我认为 DataGridComboBoxColumn 非常适合这项工作。如何使用 JarTypes.Type 填充 ComboBox 并将 TypeId 放回 Jars.TypeId。这些表存储在 DataTables JarsDt 和 TypeDt 中

罐子 (JarsDt)

  • JarId (PK)
  • 金额
  • TypeId (FK)

JarTypes (TypeDt)

  • TypeId (PK)
  • 类型

XAML

    <DataGrid x:Name="JarDataGrid"
                  AutoGenerateColumns="False"
                  IsReadOnly="False"
                  HorizontalAlignment="Stretch"
                  Margin="10,35,2,36.8">
        <DataGrid.Columns>
            <DataGridTextColumn Header="#"
                                IsReadOnly="True"
                                Binding="{Binding JarId}" />
            <DataGridTextColumn Header="Mengde"
                                Binding="{Binding Amount}" />
            <DataGridComboBoxColumn x:Name="combo"
                                    Header="Glass"
                                    SelectedValueBinding="{Binding TypeId}"
                                    DisplayMemberPath="{Binding Type}"/>
            <DataGridTextColumn Header="Mengde levert"
                                IsReadOnly="True"
                                Binding="{Binding AmountDelivered}" />

【问题讨论】:

    标签: sql wpf datagrid wpfdatagrid


    【解决方案1】:

    DataGridItemsSource 属性设置为Jar 列表,将ComboBoxItemsSource 属性设置为JarType 列表。

    您可以在 XAML 中使用 CollectionViewSource 或通过直接设置 JarDataGrid.ItemsSource = ListOfJars 在代码中执行此操作。

    您需要设置DataGridComboBoxColumnSelectedValuePath 属性。

    另外DisplayMemberPath 是一个字符串属性,所以不使用绑定。

    在你的情况下是:

    <DataGridComboBoxColumn x:Name="combo"
                            Header="Glass"
                            DataContext="{StaticResource jarTypesViewSource}"
                            ItemsSource="{Binding}"
                            SelectedValueBinding="{Binding TypeId}"
                            SelectedValuePath="TypeId"
                            DisplayMemberPath="Type"/>
    

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2012-01-23
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多