0

我有一个带有两个组合框的 Windows 窗体,每个组合框都使用 DataSet 和 TableAdapter 填充。ComboBox1包含员工的姓名并ComboBox2包含分配给所有员工的区域。我希望能够选择员工的姓名并过滤ComboBox2以仅显示员工分配的地区。

这是我到目前为止得到的:

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

我已经搜索了解决方案,但他们使用不使用 DataSet 的 SQL 查询来完成。

4

1 回答 1

0

简单的方法是使用 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;

和:

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"];
    }
}
于 2014-10-29T07:43:10.040 回答