المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ComboBox Filtering



C# Programming
05-26-2010, 07:35 AM
Hi all,

I have the following code that returns an error of "Cannot find column ['selected value of the combobox'] when I select a value in the first comboBox. Having a hard time figuring this one out. Any help would be greatly appreciated. Thanks.

public void Form1_Load(object sender, EventArgs e)
{

DataTable tblPrimary = dataSet1.Tables.Add("Primary");
tblPrimary.Columns.Add("Type");

tblPrimary.Rows.Add("Force");
tblPrimary.Rows.Add("Torque");
tblPrimary.Rows.Add("Pressure");

DataTable tblSecondary = dataSet1.Tables.Add("Secondary");
tblSecondary.Columns.Add("Primary_Type"); tblSecondary.Columns.Add("Unit");
tblSecondary.Rows.Add("Force", "lb");
tblSecondary.Rows.Add("Force", "N");
tblSecondary.Rows.Add("Force", "oz");
tblSecondary.Rows.Add("Torque", "in-lb");
tblSecondary.Rows.Add("Torque", "oz-in");
tblSecondary.Rows.Add("Torque", "N-m");

dataGridView1.DataSource = tblSecondary;

DataGridViewComboBoxColumn clmType = new DataGridViewComboBoxColumn();
clmType.DataSource = tblPrimary;
clmType.ValueMember = "Type";
dataGridView1.Columns.Add(clmType);



}//end of Form1_Load


private void primaryCB_SelectedIndexChanged(object sender, EventArgs e)
{

if (dataGridView1.CurrentCell.ColumnIndex == 0) //allow control on only one column
{

ComboBox cb = sender as ComboBox;

if (cb != null)
{
Debug.WriteLine(cb.SelectedValue + "TEST");

bsSecondary.DataSource = dataSet1.Tables["tblSecondary"];

//filter on Primary_Type column
string filter = string.Format("Primary_Type = {0}", cb.SelectedValue);

bsSecondary.Filter = filter;

Debug.WriteLine(filter);

DataGridViewComboBoxColumn clmUnit = new DataGridViewComboBoxColumn();

clmUnit.DataSource = bsSecondary;

clmUnit.DisplayMember = bsSecondary.DataMember;
dataGridView1.Columns.Add(clmUnit);



}
}
}