Reputation: 3
I have a Windows Form with two comboBoxes, each one is populated using a DataSet and a TableAdapter. ComboBox1
contains Employee's names and ComboBox2
contains the territories assigned to all employees. I would like to be able to select an employee's name and filter the ComboBox2
to display only the employee's assigned territories.
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.
Upvotes: 0
Views: 357
Reputation: 5373
the easy way is to use DataView as the source for the second ComboBox, and then filter it:
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"];
}
}
Upvotes: 0