【问题标题】:WPF Datagrid with comboboxes and loading from DataSet带有组合框和从数据集加载的 WPF 数据网格
【发布时间】:2011-09-28 18:27:49
【问题描述】:

我对 WPF 完全陌生,并试图用 DataGrid 制作一个简单的应用程序。此应用程序从 WebService 接收员工数据集,并且必须允许查看和编辑数据。 DataSet 包含三个表:Employees (id, Name, GenderId, PositionId)、Genders (id, Name) 和 Positions (Id, Name)。员工表通过 GenderId 与 Genders 连接,并通过 PositionId 与 Positions 连接。 我已经将员工数据集拖到窗口并获得了允许编辑项目的数据网格。但现在它包含位置和性别的 ID,我需要组合框,它允许从可用值中选择性别或位置并将它们保存回数据集。 我通过以下方式加载数据集:

EmployeesProxy.EmployeesServiceClient proxy = new EmployeesProxy.EmployeesServiceClient();
        proxy.ClientCredentials.UserName.UserName = "user1";
        proxy.ClientCredentials.UserName.Password = "pass1";
        empDataSet = proxy.GetEmployeesData();
        employeesDataGrid.ItemsSource = empDataSet.Employees.DefaultView;
        GenderDT = empDataSet.Gender;

        proxy.Close();

我的窗口中有 empDataSet 和 GenderDT 作为属性。 但是,我无法在 XAML 中使用允许编辑数据的组合框创建列。有没有办法做到这一点?

编辑:我发现这个解决方案部分有效(我可以在编辑模式下选择性别,但在正常模式下无法查看性别名称,我只看到 ID):

<DataGridTemplateColumn Header="Gender">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=GenderDT, 
                                                RelativeSource={RelativeSource FindAncestor, 
                                                AncestorType={x:Type Window}}}"  
                                  DisplayMemberPath="Name" 
                                  SelectedValuePath="Gender" SelectionChanged="ComboBox_SelectionChanged" Loaded="ComboBox_Loaded"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>                    
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=GenderId}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>                    
            </DataGridTemplateColumn>

更改 ComboBox 后,我更改了 Gender 列:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ((DataRowView)employeesDataGrid.SelectedItem).Row["GenderId"] = ((DataRowView)((ComboBox)sender).SelectedItem).Row["Id"];
    }

但是,就我而言,这不是一个好习惯。而且我仍然没有得到想要的结果。我认为可能有一种简单的方法来完成这样的任务。

【问题讨论】:

    标签: c# .net wpf datagrid datatable


    【解决方案1】:

    我有同样的问题并找到了解决方案。我希望这会对某人有所帮助。 首先,您必须将 DataGrid 数据绑定到主表(在您的情况下为员工):

    myDataGrid.ItemsSource = dataset.Employees.DefaultView;
    

    然后在 DataGrid 中添加一个组合并为其命名,例如:positionCombo。然后你必须将组合(positionCombo)数据绑定到相关表

    positionCombo.ItemsSource = dataset.Positions.DefaultView;
    

    之后您必须在 XAML 中指定一些属性:

    <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
       <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="positionCombo" Header="Position" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValueBinding="{Binding PositionId}"/>
       </DataGrid.Columns>
    </DataGrid>
    

    请注意,(Name, Id) 是“Positions”表中的字段,“PositionId”是“Employees”表中的 FK。

    【讨论】:

    • 我不敢相信我在一年多的时间里投了第一个赞成票 - 这很好地解决了我的问题,谢谢。
    • 这就是分享的美妙之处。当您看到您的答案挽救了某人的一天时,真的很高兴。一段时间后我回到你的评论☺。也谢谢你。
    【解决方案2】:

    您可以将 DataGridTemplateColumn 与以下内容一起使用:

     <DataGridTemplateColumn >
                         <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                               <ComboBox >
                                  ....
                               </ComboBox>
                            </DataTemplate>
                         </DataGridTemplateColumn.CellTemplate>
                      </DataGridTemplateColumn>
    

    【讨论】:

    • 是的,我正在做这样的事情,但我无法让它工作:(。请看看我的代码,我更新了问题。
    猜你喜欢
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2018-12-22
    • 2013-03-19
    • 1970-01-01
    • 2017-05-17
    • 2014-08-25
    • 2018-05-21
    相关资源
    最近更新 更多