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
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);
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,
ListViewItem itm = new ListViewItem(priorityData)
With this code
ListViewItem itm = new ListViewItem(priorityData[
Will do the trick,
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
items[1].subitems[1].text,
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[
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Closed, 125 points refunded.
Vee_Mod
Community Support Moderator
Vee_Mod
Community Support Moderator
itm.SubItems.Add()
so the medium and low will be subitems.