Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to bind a combo box to the current item and use a list of possible values to modify the item
 

How to bind a combo box to the current item and use a list of possible values to modify the item

I hope someone can point me in the right direction.

I would like to bind a combo box to (an item)of the Current row in my bindingsource and allow the user to modify this value by selecting a new value from a list of possible values. Just to be clear: I don't want to bind the combo to all items in the datasource (binding source), but to use an (external)limiting list of values allowed for this value in this row in my data.

With this all I like the value member (as stored in de db) to be different from the display member.

Anyone know a 'best practice' way to do this?

Dirk-Jan  Sunday, December 23, 2007 10:15 PM

Are the valuesfor the ComboBox from database? If so, I think you may havea table for binding the controls on the form, and another table for binding the ComboBox,see example below. (To make things easy I just create DataTable objects by code, however, you can load data from database, the logic for binding is the same)

Code Block

void Form1_Load(object sender, EventArgs e)

//dt1 for binding the controls on form

DataTable dt1 = new DataTable();

"c1");

"sid");

"aaa", 3);

"bbb", 2);

"ccc", 1);

"ddd", 4);

//dt2 for binding the ComboBox

DataTable dt2 = new DataTable();

"id",typeof(int));

"species");

"man");

"fish");

"bird");

"mamal");

//bind the ComboBox

this.comboBox1.DisplayMember = "species";

this.comboBox1.ValueMember = "id";

this.comboBox1.DataSource = dt2;

//bind controls on the form

this.textBox1.DataBindings.Add("Text", dt1, "c1", true,

DataSourceUpdateMode.OnPropertyChanged);

this.comboBox1.DataBindings.Add("SelectedValue", dt1, "sid",true,

DataSourceUpdateMode.OnPropertyChanged);



If the values for the ComboBox are not from database, i.e. you they're added manually, then you can do something like this

Code Block

void Form1_Load(object sender, EventArgs e)

//dt1 for binding the controls on form

DataTable dt1 = new DataTable();

"c1");

"species");

"aaa", "bird");

"bbb", "fish");

"ccc", "man");

"ddd", "mamal");

//populate the ComboBox

this.comboBox1.Items.Add("man");

this.comboBox1.Items.Add("fish");

this.comboBox1.Items.Add("bird");

this.comboBox1.Items.Add("mamal");

//bind controls on the form

this.textBox1.DataBindings.Add("Text", dt1, "c1", true,

DataSourceUpdateMode.OnPropertyChanged);

this.comboBox1.DataBindings.Add("SelectedItem", dt1, "species",true,

DataSourceUpdateMode.OnPropertyChanged);

You can filter the BindingSource by setting the Filter property, something like this

Code Block

privatevoid Form1_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("id", typeof(int));

dt.Columns.Add("name");

for (int j = 0; j < 10; j++)

{

dt.Rows.Add(j, "row" + j.ToString());

}

BindingSource bs = new BindingSource();

bs.DataSource = dt;

bs.Filter = "id<5";

this.comboBox1.DisplayMember = "name";

this.comboBox1.ValueMember = "id";

this.comboBox1.DataSource = bs;

}

Zhi-Xin Ye  Tuesday, December 25, 2007 11:30 AM

Thanks for answering, but I think I did not explain my problem clearly enough:

What I need is to allow the user to modify the value of a column in the Current row. I navigated somehow to this row and now the user can, on a detail form, for example change the value for the column 'species' in this row to any of the values "man", "fish", "bird", "mamal", etc., but not to any other value. The problem with a standard combo box is that it lists all the rows in my data table, which I could indeed limit with a filter. I does not list all the possible values I could enter to modify a column value of the currently, thru the bindingsource,selected row, and that is what I would like to do.

Any suggestions?

Dirk-Jan  Tuesday, December 25, 2007 11:56 AM
Do you display data in a DataGridView? Maybe you can use the DataGridViewComboBoxColumn,
Zhi-Xin Ye  Tuesday, December 25, 2007 1:14 PM

I didn't know about that one yet (still learning). Itlooks like that this combo box provides exactly the kind of functionality I am looking for.

The only problemis that I am not working in a DataViewGrid (bit user unfriendly for ordinary users), but have a form full of textboxes and such. It would be nice if there was a (seperate) form control that offered this same kind of functionality? Or an easy way to fake it?

Dirk-Jan  Tuesday, December 25, 2007 6:43 PM

Are the valuesfor the ComboBox from database? If so, I think you may havea table for binding the controls on the form, and another table for binding the ComboBox,see example below. (To make things easy I just create DataTable objects by code, however, you can load data from database, the logic for binding is the same)

Code Block

void Form1_Load(object sender, EventArgs e)

//dt1 for binding the controls on form

DataTable dt1 = new DataTable();

"c1");

"sid");

"aaa", 3);

"bbb", 2);

"ccc", 1);

"ddd", 4);

//dt2 for binding the ComboBox

DataTable dt2 = new DataTable();

"id",typeof(int));

"species");

"man");

"fish");

"bird");

"mamal");

//bind the ComboBox

this.comboBox1.DisplayMember = "species";

this.comboBox1.ValueMember = "id";

this.comboBox1.DataSource = dt2;

//bind controls on the form

this.textBox1.DataBindings.Add("Text", dt1, "c1", true,

DataSourceUpdateMode.OnPropertyChanged);

this.comboBox1.DataBindings.Add("SelectedValue", dt1, "sid",true,

DataSourceUpdateMode.OnPropertyChanged);



If the values for the ComboBox are not from database, i.e. you they're added manually, then you can do something like this

Code Block

void Form1_Load(object sender, EventArgs e)

//dt1 for binding the controls on form

DataTable dt1 = new DataTable();

"c1");

"species");

"aaa", "bird");

"bbb", "fish");

"ccc", "man");

"ddd", "mamal");

//populate the ComboBox

this.comboBox1.Items.Add("man");

this.comboBox1.Items.Add("fish");

this.comboBox1.Items.Add("bird");

this.comboBox1.Items.Add("mamal");

//bind controls on the form

this.textBox1.DataBindings.Add("Text", dt1, "c1", true,

DataSourceUpdateMode.OnPropertyChanged);

this.comboBox1.DataBindings.Add("SelectedItem", dt1, "species",true,

DataSourceUpdateMode.OnPropertyChanged);

Thanks a million, this is exactly what I needed. It works like a dream

Dirk-Jan  Wednesday, December 26, 2007 12:06 PM

You can use google to search for other answers

Custom Search

More Threads

• How to change position in a binding source.
• change column type of datagridview column after binding
• Problem with ToolStripDropDown in DataGridViewCell
• tutorial with datagridview
• How to implement transaction?
• Why more than one datagridview cells are highlighted
• Set Row Marker after modifing a row
• Across Row Tabbing in DataGridView Control
• DataGridView Scrollbar
• Creating a Database that holds fields and files