using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace WindowsApplication3
{
public partial class Form1 : Form
{
DataTable categoryDT, subCategoryDT;
BindingSource catBS, filteredSubCatBS, unfilteredSubCatBS;
public Form1()
{
categoryDT = new DataTable("category");
categoryDT.Columns.Add("ID", typeof(int));
categoryDT.Columns.Add("Name", typeof(string));
subCategoryDT = new DataTable("subcategory");
subCategoryDT.Columns.Add("ID", typeof(int));
subCategoryDT.Columns.Add("subID", typeof(int));
subCategoryDT.Columns.Add("Name", typeof(string));
categoryDT.Rows.Add(new object[] { 0, "cat0" });
categoryDT.Rows.Add(new object[] { 1, "cat1" });
categoryDT.Rows.Add(new object[] { 2, "cat2" });
subCategoryDT.Rows.Add(new object[] { 0, 0, "SubCat0-Cat0" });
subCategoryDT.Rows.Add(new object[] { 1, 0, "SubCat1-Cat0" });
subCategoryDT.Rows.Add(new object[] { 2, 0, "SubCat2-Cat0" });
subCategoryDT.Rows.Add(new object[] { 3, 1, "SubCat3-Cat1" });
subCategoryDT.Rows.Add(new object[] { 4, 1, "SubCat4-Cat1" });
subCategoryDT.Rows.Add(new object[] { 5, 1, "SubCat5-Cat1" });
subCategoryDT.Rows.Add(new object[] { 6, 2, "SubCat6-Cat2" });
subCategoryDT.Rows.Add(new object[] { 7, 2, "SubCat7-Cat2" });
subCategoryDT.Rows.Add(new object[] { 8, 2, "SubCat8-Cat2" });
InitializeComponent();
catBS = new BindingSource();
catBS.DataSource = categoryDT;
categoryComboBoxColumn.DataSource = catBS;
categoryComboBoxColumn.DisplayMember = "Name";
categoryComboBoxColumn.ValueMember = "ID";
// the ComboBox column is bound to the unfiltered DataView
unfilteredSubCatBS = new BindingSource();
DataView undv = new DataView(subCategoryDT);
unfilteredSubCatBS.DataSource = undv;
subCategoryComboBoxColumn.DataSource = unfilteredSubCatBS;
subCategoryComboBoxColumn.DisplayMember = "Name";
subCategoryComboBoxColumn.ValueMember = "ID";
// this binding source is where I perform my filtered view
filteredSubCatBS = new BindingSource();
DataView dv = new DataView(subCategoryDT);
filteredSubCatBS.DataSource = dv;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
if (e.ColumnIndex == subCategoryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = filteredSubCatBS;
this.filteredSubCatBS.Filter = "subid = " +
this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
}
}
catch { }
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == this.subCategoryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = unfilteredSubCatBS;
this.filteredSubCatBS.RemoveFilter();
}
}
catch { }
}
}
}