how can I add ComboBox to DataGridView?


#1

this is a question for the .NET experts here,

does anybody know how can I add combo box to DataGridView control?

I followed the explanation here:.NET Datatable/Datagrid question[left][/left]
[left][/left]
[left][/left]
[left][/left]

and created a column listing the names of all the objects in the scene -
that was easy, but then I wanted to add a combo box column with say 2 options “Left” and “Right” to choose from, and this is where I got stuck: how can I populate the combo box list?

I tried several methods, include attaching dataSource table to the column, but nothing seems to work (??)

please help!

I would also be happy to hear if from your experience the XPTable control is better (regarding performance) for this then the DataGridView


#2

Hello cannan,

as far as populating the combobox goes I can help you. I’ve had the same problem: http://forums.cgsociety.org/showthread.php?f=98&t=585890&

The trick is to feed the combobox .NET strings instead of plain max-strings.

Klaas


#3

to anybody who struggle with this, I also found another solution on this page:
XPTable (.NET)

---------------------------------------------------------------
 
   fn dotNetArray mxsArray = (
  	netArray = dotNetObject "System.String[]" mxsArray.count
  	
  	for i=1 to mxsArray.count do (
  		str = dotNetObject "System.String" mxsArray[i]
  		indx = dotNetObject "System.Int32" (i-1)
  		netArray.SetValue str indx
  	)
  	
  	netArray
  )
 
 -----------------------------------------------------------------
   
   
   local cb = dotNetObject "System.Windows.Forms.DataGridViewComboBoxColumn"
   cb.headerText = "some name"
   cb.Items.AddRange (dotNetArray #("one", "two", "three", "four"))

#4

Hi, regarding the use of .NET strings, it really depends on the version of Max you’re using, in 2009 and 2010 I didn’t have that conversion problem… weither way, I did a VisualBasic form reader a while ago and I had comboboxes inside datagridviews and the method I was using to add items there was the AddRange, and it worked pretty well.

Here’s an example by ypuech where I’ve only changed the add items stuff to the addrange method.

rollout dotNetComboBoxTest ".NET ComboBox Test" width:200 height:40
(
	dotNetControl comboBox "System.Windows.Forms.ComboBox" pos:[10,8] width:180 height:21
	
	on dotNetComboBoxTest open do
	(
		tItems=#("Left","Center","Right")
		
		comboBox.Items.AddRange tItems
		
		comboBox.SelectedIndex = 0
		ComboBoxStyle = dotNetClass "System.Windows.Forms.ComboBoxStyle"
		comboBox.DropDownStyle = ComboBoxStyle.DropDownList
	)
	
	on comboBox SelectedValueChanged do
	(
		if comboBox.SelectedIndex != -1 then
			print comboBox.SelectedItem
	)
)

try(destroyDialog dotNetComboBoxTest)catch()
createDialog dotNetComboBoxTest

#5

Regarding XPTable: there are some threads in this forum about XPTable. I like it, it works great, but has some flaws and bugs which I haven’t found a way around yet.


#6

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.