Link to home
Start Free TrialLog in
Avatar of graham_ball
graham_ball

asked on

DataGridViewComboBoxColumn not displaying data as expected

I'm generating a combobox in a datagridview using windows forms, but it doesn't display the data, only listviewItem{1}  (2 and 3).
Just can't see what I'm missing...
thanks,
Graham

DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
			comboboxColumn.DataPropertyName = "PRIORITY";
			comboboxColumn.HeaderText = "Priority";
			comboboxColumn.Name = "PRIORITY";
			comboboxColumn.DropDownWidth = 90;
			comboboxColumn.Width = 100;
			comboboxColumn.MaxDropDownItems = 3;
			comboboxColumn.FlatStyle = FlatStyle.Flat;
			comboboxColumn.SortMode = DataGridViewColumnSortMode.Automatic;
			string[] priorityData = new string[2];
			for (int i = 1; i <= 3; i++)
			{
				priorityData[0] = i.ToString();
				switch (i)
				{
					case 1:
						priorityData[1] = "High";
						break;
					case 2:
						priorityData[1] = "Medium";
						break;
					case 3:
						priorityData[1] = "Low";
						break;
					default:
						break;
				}
				ListViewItem itm = new ListViewItem(priorityData);
				itm.Selected = false;
				comboboxColumn.Items.Add(itm);
			}
			comboboxColumn.ValueMember = "Key";
			comboboxColumn.DisplayMember = "Value";
			dgvSoNInvestment.Columns.Add(comboboxColumn);

Open in new window

Avatar of p_davis
p_davis

after the first listviewitem itm addition. i believe that you will have to add the rest as
itm.SubItems.Add()

so the medium and low will be subitems.
You have created a 2 dimensional array and are only using the first one, replace your code below
ListViewItem itm = new ListViewItem(priorityData);

With this code
ListViewItem itm = new ListViewItem(priorityData[1]);

Will do the trick,
Avatar of graham_ball

ASKER

p_davis; adding items via an array automatically produces subitems that can be referenced as
items[1].subitems[1].text, so I don't need to actually add them as subitems (this works OK elsewhere in my code).

RobbP: I need a 2D array because I have a key/value combination.
when I use your code the dropdown displays
ListviewItem: {High}
ListviewItem: {Medium}
ListviewItem: {Low}
If I change it to ListViewItem itm = new ListViewItem(priorityData[0]);
I get
ListviewItem: {1}
ListviewItem: {2}
ListviewItem: {3}

I want both values in the combobox so that the underlying priority value (1,2,3) will display the correct text. (and the cell should be updated with the correct value when an enrty is selected).

cheers,
Graham
ASKER CERTIFIED SOLUTION
Avatar of graham_ball
graham_ball

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Closed, 125 points refunded.
Vee_Mod
Community Support Moderator