DataGridComboBoxColumn使用AutoGeneratingColumn事件

时间:2017-11-04 22:40:49

标签: c# wpf data-binding datagrid

我正在尝试在DataGrid控件中创建AutoGenerateColumns,并将AutoGeneratingColumn属性设置为true。我正在使用DataGridComboBoxColumn事件,我可以轻松更改网格标题,但我无法正确设置CREATE TABLE Department( dptID int IDENTITY(1,1) NOT NULL PRIMARY KEY, dptName nvarchar(30) NOT NULL ) CREATE TABLE Employees( empID int IDENTITY(1,1) NOT NULL PRIMARY KEY, empName nvarchar(30) NOT NULL, empDptID int NOT NULL ) ALTER TABLE Employees WITH CHECK ADD CONSTRAINT FK_Emp_DptID FOREIGN KEY(empDptID) REFERENCES Department(dptID) ALTER TABLE Employees CHECK CONSTRAINT FK_Emp_DptID (及其绑定)。我需要这个让用户从另一个数据库表(外键)中选择数据,但在组合框中显示不同的字段。

为了便于在此论坛中进行演示和代码插入,我已将数据库定义和数据网格简化为基本要素。

这是我的数据库:

dptName

也就是说,我希望创建一个组合框列,显示部门列表(empDptID),并在dptID中存储从AutoGenerateColumns字段获取的值。

我正在使用非常小的xaml,并在后面的代码中完成所有操作。以下是我的xaml(true<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFFKTest" x:Class="WPFFKTest.ArcEmp" mc:Ignorable="d" Title="ArcEmp" Initialized="Page_Initialized"> <DataGrid x:Name="grdEmp" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" AutoGeneratingColumn="grdEmp_AutoGeneratingColumn" /> </Page> - 默认值):

namespace WPFFKTest
{
    /// <summary>
    /// Interaction logic for ArcEmp.xaml
    /// </summary>

    public partial class ArcEmp : Page
    {
        private DataTable dtEmp = null, dtDpt = null;

        public ArcEmp()
        {
            InitializeComponent();
        }

        private void Page_Initialized(object sender, EventArgs e)
        {
            dtDpt = OpenDBTable("Department");
            dtEmp = OpenDBTable("Employees");
            grdEmp.ItemsSource = dtEmp.DefaultView;
        }

        private void grdEmp_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header as string == "empDptID")
            {
                try
                {
                    DataGridComboBoxColumn col = new DataGridComboBoxColumn();
                    col.Header = "Department";

                    // This actually works
                    col.ItemsSource = dtDpt.AsEnumerable().Select(r => r["dptName"]).ToList();
                    Binding cbi = new Binding("empDptID"); // Not sure what name to put here

                    // This code does not work, and I don't know what to do
                    col.SelectedValueBinding = cbi;
                    col.SelectedValuePath = "dptID";
                    //col.DisplayMemberPath = "dptName";

                    // Replace the auto-generated column with the new one.
                    e.Column = col;
                }
                catch (Exception x)
                {
                    System.Windows.MessageBox.Show(x.Message);
                }
            }
            else if ((e.Column.Header as string == "empID"))
                e.Column.Header = "ID";
            else if ((e.Column.Header as string == "empName"))
                e.Column.Header = "Name";
        }

    }
}

我的代码:

{{1}}

这里唯一真正有用的是组合框中填充了部门名称。它不会显示在网格中,也不会更新。搜索了很多,但无法找到如何正确设置绑定。

有人可以帮忙吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

private void grdEmp_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header as string == "empDptID")
    {
        try
        {
            DataGridComboBoxColumn col = new DataGridComboBoxColumn();
            col.Header = "Department";

            // This actually works
            col.ItemsSource = dtDpt.DefaultView;
            col.SelectedValueBinding = new Binding("empDptID");
            col.SelectedValuePath = "dptID";
            col.DisplayMemberPath = "dptName";

            // Replace the auto-generated column with the new one.
            e.Column = col;
        }
        catch (Exception x)
        {
            System.Windows.MessageBox.Show(x.Message);
        }
    }
    else if ((e.Column.Header as string == "empID"))
        e.Column.Header = "ID";
    else if ((e.Column.Header as string == "empName"))
        e.Column.Header = "Name";
}