PDA

View Full Version : C# databinding question for the programmers here



ctmal
05-25-2012, 11:16 PM
Iím in the process of upgrading an AddIn and Iíve decided I want to use databinding to Ďmake my life easierí in my windows forms. Iím running into some issues and Iím hoping someone can help me wrap my head around this.

Iím using a DataGridView and Iíve been able to bind textboxes, checkboxes, etc. but I canít seem to get a combobox to work.

The background infoÖ
The AddIn receives a list of Channels from Vixen. I need to be able to populate the dropdown of the combobox with the Names of the channels. I thought I could just do an items.addrange but that doesnít seem to be working.
When a user selects the channel name, I need to save either the entire channel object or the channelís output channel to a custom object in my AddIn so I can use it later. The channel name itself wonít work since it isnít required to be unique in Vixen.

So, the issue I canít seem to figure out is how do I bind the dropdown list to the sequence channelname, and the values to be saved to my custom class?

I hope I explained that well enoughÖ

Thanks,
Chris

ctmal
05-25-2012, 11:18 PM
Also, if I could just get the index of the selected item, that would work for me too.

djulien
05-26-2012, 10:26 PM
Also, if I could just get the index of the selected item, that would work for me too.

Usually there is a SelectedIndex property or equivalent. (I'm not familiar with DataGridView specifically).

Can you post a sample code snipet?

don

ctmal
05-27-2012, 10:25 AM
Usually there is a SelectedIndex property or equivalent. (I'm not familiar with DataGridView specifically).

Can you post a sample code snipet?

don

Apparently there isn't a selected index with the datagridviewcombo box...that would make things too easy. I'll post some code later today.

ctmal
05-27-2012, 05:55 PM
Ok, I think I've got it figured out. For future reference(when I forget how I did it), here's what needed to be done

• Set the DataGridView.Datasource to my custom Object.
• Set the ComboBox.DataPropertyName to “OutputChannel”. This is a property within by custom object…the one that I want written.
• Set the ComboBox.DataSource to the EventSequence.Channels property. This is the 2nd Object that I want to tie my Items to.
• Set the ComboBox.Display = “Name”. this is the name property within the EventSequence.Channels object. It tells the ComboBox to populate the itemList with Names(so the user can understand what they’re looking at).
• Set the ComboBox.ValueMember = “OutputChannel”. This is also a property in the EventSequence.Channels object. It tells the combobox to send the objects OutputChannel instead of it’s name when an item is selected.

So much for making my life easier this time around but it might help down the road.