使用AutoGeneratingColumn事件的DataGridComboBoxColumn [英] DataGridComboBoxColumn Using the AutoGeneratingColumn Event

查看:120
本文介绍了使用AutoGeneratingColumn事件的DataGridComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在具有 AutoGenerateColumns的 DataGrid 控件中创建一个 DataGridComboBoxColumn 属性设置为true。我正在使用 AutoGeneratingColumn 事件,并且可以轻松更改网格标题,但是我无法正确设置 DataGridComboBoxColumn (及其绑定)。我需要让用户从另一个数据库表(外键)中选择数据,但要在组合框中显示一个不同的字段。

I'm trying to create a DataGridComboBoxColumn in a DataGrid control having the AutoGenerateColumns property set to true. I'm using the AutoGeneratingColumn event, and I can easily change the grid headers, but I'm having trouble to correctly setup a DataGridComboBoxColumn (and its bindings). I need this to have the user choosing data from another database table (foreign key), but displaying a different field in the combo-box.

为了便于演示和这个论坛中的代码方面,我已经将数据库定义和datagrid简化为基本要素。

For the purposes of easier presentation and code inpections in this forum, i have stripped down the database definitions and datagrid to just the essentials.

这是我的数据库:

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 ),并将从 dptID 字段获取的值存储在 empDptID 中。

That is, I wish to create a combo-box column, displaying a list of the departments (dptName), and storing in empDptID a value acquired from the dptID field.

我使用的是最小的xaml,并在后面的代码中完成了所有操作。以下是我的xaml( AutoGenerateColumns true -默认):

I'm using very minimal xaml, and having everything done in the code behind. Below is my xaml (AutoGenerateColumns is true - default):

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

    }
}

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

The only thing that really works here is that the combo-box is filled with the department names. It's not displayed in the grid, neither it is updated. Searched a lot, but couldn't find how to setup the binding correctly.

有人可以帮忙吗?

谢谢您

推荐答案

尝试一下:

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

这篇关于使用AutoGeneratingColumn事件的DataGridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆