简体   繁体   English

使用WPF表单DataGrid,DataTable和Combobox

[英]Working with WPF Form DataGrid, DataTable and Combobox

I am using WPF. 我正在使用WPF。 I am trying to use a grid to show table within my database. 我正在尝试使用网格来显示数据库中的表。 The goal is to have all the data in that grid. 目标是将所有数据存储在该网格中。 Example: 例:

ID Firstname Lastname ID名姓
1 John Smith 1约翰·史密斯
2 Jane Smith 2简·史密斯
However, each cell should be a combobox that has every choice for that particular column if clicked. 但是,每个单元格应该是一个组合框,如果单击该框,则该特定列具有所有选择。 So clicking John will display combo box with every firstname in the table, in this case it would say John and Jane. 因此,单击“ John”将显示表中每个名字的组合框,在这种情况下,它将显示John和Jane。 If the user chose to click on ID , it would display 1 and 2 and so forth. 如果用户选择单击ID,它将显示1和2,依此类推。

What i have tried so far is to use a data table as the datagrids item source. 到目前为止,我尝试过使用数据表作为datagrids项源。 This works perfectly, but I cannot add a combobox to a datatable. 这可以完美地工作,但是我无法将组合框添加到数据表中。 I can add a combobox column to the datagrid, but then I am no longer using the data table and am not sure how to iterate through each row in the database with using a comboboxcolumn. 我可以将combobox列添加到datagrid,但是随后我不再使用数据表,并且不确定如何使用comboboxcolumn遍历数据库中的每一行。

So what I want is a combobox in each of the cells which shows the corresponding data for that particular row but upon clicking it will list all choices. 因此,我想要的是每个单元格中的一个组合框,该组合框显示该特定行的相应数据,但是单击后将列出所有选择。 I have searched around but I am not sure I am searching for the right things. 我已经搜索了周围,但不确定是否正在搜索正确的东西。

I have tried a few things here and there with the combo boxes but nothing worth noting. 我已经使用组合框在这里和那里尝试了一些东西,但是没有什么值得注意的。 Also, I have auto generated columns, not sure if you can have non auto generated columns and still use a binding or how to define it, though. 另外,我有自动生成的列,不确定是否可以有非自动生成的列,但仍然使用绑定或如何定义它。

This is the data table generating. 这是数据表的生成。

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; 
 }

This ran when a user clicks on an item in a list box, it binds the data table. 当用户单击列表框中的项目时,将运行此操作,它将绑定数据表。

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();
}

I would create my data object behind my DataGrid with the following properties 我将在DataGrid后面创建具有以下属性的数据对象

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

Then bind my DataGrid using TemplateColumns that have ComboBoxes which bind to the collections of values in the DataContext, like this: 然后使用具有ComboBox的TemplateColumns绑定我的DataGrid,该ComboBox绑定到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>

And I'd populate my lists at the time the grid loads (and maybe update it as items change if needed) 而且我会在网格加载时填充我的列表(如果需要,可以在项目更改时对其进行更新)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM