简体   繁体   English

C#-如何使用DataSet通过ComboBox1的ValueMember筛选ComboBox2数据

[英]C# - How to filter ComboBox2 data by ValueMember of ComboBox1 using DataSet

I have a Windows Form with two comboBoxes, each one is populated using a DataSet and a TableAdapter. 我有一个带有两个comboBox的Windows窗体,每个组合都使用DataSet和TableAdapter填充。 ComboBox1 contains Employee's names and ComboBox2 contains the territories assigned to all employees. ComboBox1包含员工的姓名, ComboBox2包含分配给所有员工的地区。 I would like to be able to select an employee's name and filter the ComboBox2 to display only the employee's assigned territories. 我希望能够选择一个雇员的姓名并过滤ComboBox2以仅显示该雇员的分配地区。

Here is what I got so far: 这是到目前为止我得到的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Prueba2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void LlenaEmpleados()
    {
        DataSet1 ds = new DataSet1();
        DataSet1TableAdapters.EmployeesTableAdapter Adapter = new DataSet1TableAdapters.EmployeesTableAdapter();

        Adapter.FillEmployees(ds.Employees);

        ds.Employees.Columns.Add("FullName", typeof(string), "FirstName +' '+ LastName");

        cbPrimero.DataSource = ds.Tables["Employees"];
        cbPrimero.DisplayMember = "FullName";
        cbPrimero.ValueMember = "EmployeeID";
    }
    public void LlenaTerritorios()
    {
        DataSet1 ds = new DataSet1();
        DataSet1TableAdapters.TerrioriosTableAdapter Adapter = new DataSet1TableAdapters.TerrioriosTableAdapter();

        Adapter.FillTerritorios(ds.Territorios);

        cbSegundo.DataSource = ds.Tables["Territorios"];
        cbSegundo.DisplayMember = "TerritoryDescription";
        cbSegundo.ValueMember = "EmployeeID";
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        LlenaEmpleados();
        cbPrimero.Text = "";

        LlenaTerritorios();
        cbSegundo.Text = "";
    }

    private void cbPrimero_SelectedIndexChanged(object sender, EventArgs e)
    {
        cbPrimero.AutoCompleteSource = AutoCompleteSource.ListItems;
        cbPrimero.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    }
}
}

I have searched for the solution, But they do it with a SQL query not using the DataSet. 我已经搜索了解决方案,但是他们通过不使用DataSet的SQL查询来实现。

the easy way is to use DataView as the source for the second ComboBox, and then filter it: 最简单的方法是将DataView用作第二个ComboBox的源,然后对其进行过滤:

    DataTable tab1 = new DataTable(); 
    tab1.Columns.Add("Name",typeof(string));
    tab1.Columns.Add("Value", typeof(string));
    tab1.Columns.Add("ID", typeof(int));

    tab1.Rows.Add("name1", "value1", 1);
    tab1.Rows.Add("name2", "value2", 2);
    tab1.Rows.Add("name3", "value3", 3);

    comboBox1.DataSource = tab1;
    comboBox1.ValueMember = "Value";
    comboBox1.DisplayMember = "Name";

    DataTable tab2 = new DataTable();
    tab2.Columns.Add("Name", typeof(string));
    tab2.Columns.Add("Value", typeof(string));
    tab2.Columns.Add("Tab1_ID", typeof(int));

    tab2.Rows.Add("1_name1", "_value1", 1);
    tab2.Rows.Add("1_name2", "_value2", 1);
    tab2.Rows.Add("1_name3", "_value3", 1);

    tab2.Rows.Add("2_name1", "_value1", 2);
    tab2.Rows.Add("2_name2", "_value2", 2);
    tab2.Rows.Add("2_name3", "_value3", 2);

    tab2.Rows.Add("3_name1", "_value1", 3);
    tab2.Rows.Add("3_name2", "_value2", 3);

    // here use DataView instaed of directly using the table:
    comboBox2.DataSource = tab2.DefaultView;
    comboBox2.ValueMember = "Value";
    comboBox2.DisplayMember = "Name";

    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    comboBox1.SelectedIndex = 0;

and: 和:

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex != -1 && comboBox1.SelectedItem != null)
    {
        (comboBox2.DataSource as DataView).RowFilter = "Tab1_ID=" + (comboBox1.SelectedItem as DataRowView).Row["ID"];
    }
}

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

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