c# - 使用 WPF 窗体 DataGrid、DataTable 和 Combobox

标签 c# wpf datagrid combobox

我正在使用 WPF。我正在尝试使用网格在我的数据库中显示表格。目标是让所有数据都在该网格中。示例:

ID 名字 姓氏
1 约翰·史密斯
2 简·史密斯
但是,每个单元格都应该是一个组合框,如果单击该组合框,则该组合框具有该特定列的所有选择。因此,单击 John 将显示包含表中每个名字的组合框,在本例中为 John 和 Jane。如果用户选择点击 ID ,它将显示 1 和 2 等等。

到目前为止,我尝试过的是使用数据表作为数据网格项目源。这非常有效,但我无法将组合框添加到数据表中。我可以将一个组合框列添加到数据网格,但是我不再使用数据表并且不确定如何使用组合框列遍历数据库中的每一行。

所以我想要的是每个单元格中的组合框,它显示该特定行的相应数据,但单击它会列出所有选项。我四处搜索,但我不确定我是否在搜索正确的东西。

我在这里和那里尝试了一些组合框的东西,但没有什么值得注意的。此外,我有自动生成的列,但不确定您是否可以拥有非自动生成的列并仍然使用绑定(bind)或如何定义它。

这是生成的数据表。

public DataTable PersonData()
{    
    List<Person> str4 = new List<Person>();
    DataTable _PersonData;

    _PersonData = new DataTable();
    _PersonData.Columns.Add(new DataColumn("FirstName", typeof(string)));
    _PersonData.Columns.Add(new DataColumn("LastName", typeof(string)));

    str4 = newquery();
    str4.ForEach(delegate(Person person1)
    {
         row3 = _PersonData.NewRow();
         _PersonData.Rows.Add(row3);
         row3["FirstName"] = person1.FirstName;
         row3["Lastname"] = person1.Lastname;
    });

    return _PersonData; 
 }

这会在用户单击列表框中的项目时运行,它会绑定(bind)数据表。

private void youclickedon(String result)
{
     newdatatable = PersonData();
    Binding binding = new Binding() {Mode=BindingMode.OneWay, Source = newdatatable, Path = new PropertyPath(".") };
    BindingOperations.SetBinding(GridData, DataGrid.ItemsSourceProperty, binding);
    GridData.Columns[0].IsReadOnly = true;
    newdatatable.AcceptChanges();
}

最佳答案

我会在 DataGrid 后面创建我的数据对象具有以下属性

  • ObservableCollection<MyObject> Records
  • List<int> Ids
  • List<string> FirstNames
  • List<string> LastNames

然后使用具有 ComboBox 的 TemplateColumns 绑定(bind)我的 DataGrid,这些 ComboBox 绑定(bind)到 DataContext 中的值集合,如下所示:

<DataGrid ItemsSource="{Binding Records}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Ids}"
                              SelectedItem="{Binding Id}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.FirstNames}"
                              SelectedItem="{Binding FirstName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.LastNames}"
                              SelectedItem="{Binding LastName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我会在网格加载时填充我的列表(如果需要,可能会在项目更改时更新它)

Ids = Records.Select(p => p.Id).ToList();
FirstNames = Records.Select(p => p.FirstName).ToList();
LastNames = Records.Select(p => p.LastName).ToList();

关于c# - 使用 WPF 窗体 DataGrid、DataTable 和 Combobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503131/

相关文章:

c# - 如何从 Asp Net Core WebApi 将日志存储到 Azure 表存储?

c# - 通过数据键名获取 Gridview 行

c# - 使用 RS256 PII 的 JWT SecurityTokenInvalidSignatureException 被隐藏

c# - WPF 中 DisplayFormatAttribute 的推荐替代方法是什么?

c# - 相当于WPF中的InvokeRequired

c# - 在 wpf 数据网格中对所有行进行排序后触发哪个事件。

c# - 错误 : the type or namespace UI doesnt exist in the namespace

wpf - 将 Entity Framework 与 WPF 数据绑定(bind)结合使用的最佳实践

c# - 突出显示 WPF 数据网格中的单元格时没有可见文本

c# - 预加载 WPF DataGrid