【问题标题】:Add ComboBox to WPF DataGrid that pulls from a SQL DataTable将 ComboBox 添加到从 SQL 数据表中提取的 WPF DataGrid
【发布时间】:2016-09-06 17:41:49
【问题描述】:

我有一个 WPF 程序,它允许用户在给定零件编号的情况下编辑 SQL 数据表以获取零件详细信息。用户输入零件号,我使用表格适配器将零件号详细信息显示到使用行过滤器的数据网格中。我希望能够获取 ObservableCollection 列表并将其绑定到我的数据网格中的某个列(即,部件类型列有一个部件类型的选择列表下拉列表)。这是我的 .cs:

public MainWindow()
        {
            createDropDowns();
            context = new RefreshAppContext();            
            InitializeComponent();
            PartTypeComboBox.ItemsSource = partTypesList;
#if DEBUG
#endif
        }

        public void findButton_Click(object sender, RoutedEventArgs e)
        {
            var partNumber = inputBox.Text;
            // Searches for part number in db.
            var foundPart = context.RefreshPartTypes.Where(x => x.PartNumber == partNumber).ToList();
            if (!foundPart.Any() == true)
            {
                MessageBox.Show("Part not found. Please try another number.");
            }
            else
            {
                // Adapter used to fill DT with info from DB based on query(ies).
                adapter_PT.Fill(table_PT);
                DataView dv = table_PT.DefaultView;
                dv.RowFilter = "PartNumber='" + partNumber + "'";

                // Fills data grid in UI.
                dg.DataContext = dv;
                PartTypeComboBox.ItemsSource = table_LV.DefaultView;
            }
        }
public void createDropDowns()
        {
            // Populate part styles from lookup table.
            partTypesList = new ObservableCollection<string>();
            adapter_LV.Fill(table_LV);
            List<string> temp = table_LV.AsEnumerable().Select(x => x[2].ToString()).ToList();
            foreach (var partType in temp)
            {
                partTypesList.Add(partType);
            }
            partTypesList.Add(""); // Add a blank option in case user needs to input blank cell.
        }

这是我的 .xaml:

<DataGrid x:Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="28,-145,-779,0" VerticalAlignment="Top" Height="165" Width="1222" AlternatingRowBackground="LightGray" AlternationCount="2">
            <DataGrid.Columns>
                <DataGridComboBoxColumn x:Name="PartTypeComboBox" 
                                        Header="PartTypeTest" 
                                        DisplayMemberPath="Key" 
                                        SelectedValuePath="Id" 
                                        SelectedValueBinding="{Binding PartType}"/>
            </DataGrid.Columns>
        </DataGrid>

我可以让它使用正确的信息列表创建一个新的 ComboBox 列,但我无法让该组合框显示(或绑定?)到从我的 table_PT 数据表填充的 PartType 列上。方向?谢谢!

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    您需要将DisplayMemberPath 指定为每个项目的显示字符串属性的路径。

     <DataGridComboBoxColumn x:Name="PartStylesComboBox"  DisplayMemberPath="PartType "  Header="Test" SelectedValuePath="{Binding partStylesList}" />
    

    【讨论】:

    • 通过该编辑,我无法再看到我附加到组合框中的项目列表。它只是给了我一个空的下拉框。还有什么建议吗?谢谢!
    • @KaylaNinh 是否显示这些值?
    • 很遗憾,它没有显示任何值
    • 应该的,如果你能给团队查看器什么的,我可以检查
    • 很遗憾我不能。当我将 DisplayMemberPath 设置为“Key”时,它只显示正确的组合框列表。我的问题似乎是将它连接到我的数据网格上的列上。它不是附加到 PartType 列上,而是使其成为自己的列。我是否应该为我的数据网格设置 SlectedValuePath、SelectedValueBinding 或 SelectedItemBinding?
    【解决方案2】:

    已解决:

    <DataGrid x:Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="event_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="28,-145,-134,0" VerticalAlignment="Top" Height="165" Width="582" AlternatingRowBackground="LightGray" AlternationCount="2">
                <DataGrid.Columns>
                    <DataGridTemplateColumn x:Name="comboCol" Header="PartTypeTest" Width="150">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding partTypesList, RelativeSource={RelativeSource AncestorType=Window}}" 
                                          SelectedItem="{Binding PartType, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 2021-11-16
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 2016-04-30
      • 2015-10-09
      • 2021-03-29
      • 2011-05-06
      相关资源
      最近更新 更多