We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Comboboxes and Data sources...

Hi, I have a collection of objects which I am trying to use as a datasource for my grid (1 row per object). My problem is that I dont know how to populate the combobox with options, and then have the combobox select the value that each object holds. Grid.Item(Row, 3).CellType = "ComboBox" Grid.Item(Row, 3).DataSource = MyObject Grid.Item(Row, 3).DisplayMember = "ItemType" Grid.Item(Row, 3).ValueMember = "idItemType" Grid.Item(Row, 3).ExclusiveChoiceList = True If you can imagine the scenario above .... the property "ItemType" is the description I want them to select, but idItemType is the value that needs to be stored (ItemType is a readonly string property). How do I populate the combo with values and then have it default to the value of the object it is bound to? eg. 1 CPU 2 Motherboard 3 RAM 4 Network if MyObject.idItemType = 3, then the combobox should say "RAM" Thanks

4 Replies

AD Administrator Syncfusion Team June 22, 2003 11:31 PM UTC

> Hi, > > I have a collection of objects which I am trying to use as a datasource for my grid (1 row per object). My problem is that I dont know how to populate the combobox with options, and then have the combobox select the value that each object holds. > > Grid.Item(Row, 3).CellType = "ComboBox" > Grid.Item(Row, 3).DataSource = MyObject > Grid.Item(Row, 3).DisplayMember = "ItemType" > Grid.Item(Row, 3).ValueMember = "idItemType" > Grid.Item(Row, 3).ExclusiveChoiceList = True > > If you can imagine the scenario above .... the property "ItemType" is the description I want them to select, but idItemType is the value that needs to be stored (ItemType is a readonly string property). How do I populate the combo with values and then have it default to the value of the object it is bound to? > > eg. > 1 CPU > 2 Motherboard > 3 RAM > 4 Network > > if MyObject.idItemType = 3, then the combobox should say "RAM" > > Thanks What you could do is create a class that has public properties for idItemType and ItemType. The properties should be public and browsable. E.g: public class MyData { private string id; private string desc; public string idItemType { get { return id; } } public string ItemType { get { return desc; } } } Then populate the collection, e.g. ArrayList al = new ArrayList(); al.Add(new MyDataSource(1, "RAM")); ... then you can assign this collection to a combobox: Grid.Item(Row, 3).CellType = "ComboBox" > Grid.Item(Row, 3).DataSource = al > Grid.Item(Row, 3).DisplayMember = "ItemType" > Grid.Item(Row, 3).ValueMember = "idItemType" > Grid.Item(Row, 3).ExclusiveChoiceList = True Stefan


DE Denis June 23, 2003 03:13 AM UTC

Thanks for your reply Stefan ... I understand what you are saying, however I dont want to use the arraylist object as the datasource. This does give me the drop down list as expected, however the values are not bound to the idItemType property of my MyObject class .. ie. when the combo is changed, the value in my class is not changed. Here is the code I have implemented so far: Dim arylst As New ArrayList() arylst.Add(New cComboboxDisplay("Left", CP_Just_Left)) arylst.Add(New cComboboxDisplay("Centre", CP_Just_Centre)) arylst.Add(New cComboboxDisplay("Right", CP_Just_Right)) Grid(Row, COL_Justification).CellType = "ComboBox" Grid(Row, COL_Justification).DataSource = arylst Grid(Row, COL_Justification).DisplayMember = "DisplayMember" Grid(Row, COL_Justification).ValueMember = "ValueMember" Grid(Row, COL_Justification).ExclusiveChoiceList = True This does give me the combobox with values, however I then have to have interpreting code to fill the values from the class to the combobox .. ie. Select Case Justification Case CP_Just_Centre Grid.Item(Row, COL_Justification).Text = "Centre" Case CP_Just_Left Grid.Item(Row, COL_Justification).Text = "Left" Case CP_Just_Right Grid.Item(Row, COL_Justification).Text = "Right" End Select whereas I was hoping to bind the combo to the object from MyClass and provide it with an arraylist of options ... it is probably not possible now that I think of it :/ Thanks again, D.


DE Denis June 23, 2003 03:21 AM UTC

ps. I tried to use Grid.Item(Row, COL_Justification).CellValue = Justification instead of that case statement, but it doesnt seem to work. Reading from ".CellValue" seems to work though, so a case isnt required when reading the value back into the class manually. D.


AD Administrator Syncfusion Team June 23, 2003 05:26 AM UTC

Is Grid a virtual grid where you provide values in QueryCellInfo? If so, you woul dhave to handle SaveCellInfo to move changes values back to your datasource for Grid. If you don't do this, this would explain why trying to assign Grid.Item(Row, COL_Justification).CellValue does not work. Another thing to check is whether the cell at (Row, COL_Justification) is ReadOnly. If it is you would have to set Grid.IgnoreReadOnly = true while you try to change its value.

Loader.
Live Chat Icon For mobile
Up arrow icon