在DataGridView中过滤外键

时间:2018-03-06 16:16:23

标签: c# winforms datatable foreign-keys datagridviewcomboboxcell

我正在使用C#和DataGridView WinForms,绑定到DataTable。我的DataGridView包含以下列:

  • MaterialId(只读外键显示素材标题)
  • PricelistId(带可选外键的DataGridViewComboBoxCell)

每种材料都有属于它的特定价格表。此信息存在于我的价格表中。

目前当我点击PricelistId组合时,我得到所有的价格表,无论它们属于哪种材料。但我想根据MaterialId值过滤这个。因此,例如,如果第一行上的材料是钢材,则只应在价格表字段中显示钢质价格表。如果第二行的材料是塑料,则只有与塑料相关的刻度线应显示在相应的组合中。

这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

基于@RezaAghaei的评论,这里是根据我的需求量身定制的C#原型,也解决了一个问题(当前选择的项目会重置)。

谢谢!

public partial class Form1 : Form
{
    private DataGridView _dataGridView = new DataGridView();
    private DataSet _dataSet = new DataSet();


    public Form1()
    {
        InitializeComponent();

        GenerateDataSet();
        CreateDataGridView();
    }


    private void CreateDataGridView()
    {
        var MaterialsColumn = new DataGridViewComboBoxColumn
        {
            DataPropertyName = "MaterialId",
            HeaderText = "Material",
            DataSource = _dataSet.Tables["Materials"],
            DisplayMember = "Name",
            ValueMember = "Id",
            Name = "MaterialColumn",
            ReadOnly = true
        };

        var PricelistsColumn = new DataGridViewComboBoxColumn
        {
            DataPropertyName = "PricelistId",
            HeaderText = "Pricelist",
            DataSource = _dataSet.Tables["Pricelists"],
            DisplayMember = "Name",
            ValueMember = "Id",
            Name = "PricelistColumn"
        };

        _dataGridView.AutoGenerateColumns = false;
        _dataGridView.Columns.AddRange(MaterialsColumn, PricelistsColumn);
        _dataGridView.DataSource = _dataSet.Tables["PricelistAssignments"];

        _dataGridView.Dock = DockStyle.Fill;
        _dataGridView.EditingControlShowing += EditingControlShowing;
        _dataGridView.AllowUserToAddRows = false;
        _dataGridView.AllowUserToDeleteRows = false;
        Controls.Add(_dataGridView);
    }


    private void GenerateDataSet()
    {
        // Create tables
        _dataSet.Tables.Add("Materials");
        _dataSet.Tables["Materials"].Columns.Add("Id", typeof(int));
        _dataSet.Tables["Materials"].Columns.Add("Name", typeof(string));

        _dataSet.Tables.Add("Pricelists");
        _dataSet.Tables["Pricelists"].Columns.Add("Id", typeof(int));
        _dataSet.Tables["Pricelists"].Columns.Add("Name", typeof(string));
        _dataSet.Tables["Pricelists"].Columns.Add("MaterialId", typeof(int));

        _dataSet.Tables.Add("PricelistAssignments");
        _dataSet.Tables["PricelistAssignments"].Columns.Add("MaterialId", typeof(int));
        _dataSet.Tables["PricelistAssignments"].Columns.Add("PricelistId", typeof(int));

        // Populate tables
        _dataSet.Tables["Materials"].Rows.Add(1, "Steel");
        _dataSet.Tables["Materials"].Rows.Add(2, "Plastic");
        _dataSet.Tables["Pricelists"].Rows.Add(1, "PL Steel 1", 1);
        _dataSet.Tables["Pricelists"].Rows.Add(2, "PL Plastic 1", 2);
        _dataSet.Tables["Pricelists"].Rows.Add(3, "PL Steel 2", 1);
        _dataSet.Tables["Pricelists"].Rows.Add(4, "PL Plastic 2", 2);
        _dataSet.Tables["Pricelists"].Rows.Add(5, "PL Plastic 3", 2);
        _dataSet.Tables["PricelistAssignments"].Rows.Add(1, 3);
        _dataSet.Tables["PricelistAssignments"].Rows.Add(1, 1);
        _dataSet.Tables["PricelistAssignments"].Rows.Add(2, 4);
    }


    private void EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        var grid = (DataGridView)sender;

        if (grid.CurrentCell.ColumnIndex == 1) // Pricelist column
        {
            var combo = (DataGridViewComboBoxEditingControl)e.Control;
            var selectedValue = combo.SelectedValue;
            var data = _dataSet.Tables["Pricelists"].AsDataView();
            data.RowFilter = "MaterialId = " + grid.CurrentRow.Cells[0].Value.ToString();
            combo.DataSource = data;
            combo.SelectedValue = selectedValue;
        }
    }
}