How to get DisplayMember value in drawItem comboBox event? WITH#

Please note the following:

I populate my comboBox using the following method:

void populateComboBox() { comboBox1.DataSource = GetDataTableSource(); // some data table used as source comboBox1.DisplayMember = "name"; // string comboBox1.ValueMember = "id"; // id is an int // Suppose I have this data in my comboBox after populating it // // // id (ValueMember) | name (DisplayMember) // ----------------------------------------- // 1 | name1 // 2 | name2 // 3 | name3 } 

In the DrawItem event DrawItem I want to get the value DisplayMember (name) comboBox and assign it to some variable. So far I have received this code and did not seem to work ... Please fix it. Thanks in advance.

 void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { string name = ((System.Data.DataRowView)(comboBox1.SelectedValue = e.Index))["name"].ToString(); // do something // } 
+4
source share
3 answers

Ok, I just stumbled upon this problem myself, and here is an even better answer:

 string displayValue = this.GetItemText(this.Items[e.Index]); g.DrawString(displayValue, e.Font, br, e.Bounds.Left, y + 1); 

In MSDN:

If the DisplayMember property is not specified, the value returned by GetItemText is the value of the ToString method of the element. Otherwise, the method returns the string value of the element specified in the DisplayMember property for the object specified in the item parameter

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.getitemtext(v=vs.80).aspx

+9
source

how to use only combobox element, it displays the displayed value:

 string name = (string)comboBox1.Items[e.Index]; 

If you get e.Index = -1 , change DrawMode = OwnerDrawVariable and DropDownStyle = DropDown

EDIT:

Ok, I realized what happened. I tested with strings as a data source, so the following should work in your code:

 string name = ((DataRowView)comboBox1.Items[e.Index])["name"]; 
+1
source

If you want to create a truly general function, you can calculate a little:

Suppose you have two comboboxes, one of which contains elements based on user collection A with DisplayMember AA, and the other contains elements based on user collection B with DisplayMember BB:

How can a common function know what value needs to be returned? Based on DisplayMember, of course, but you don’t want to pass AA / BB into a common function if you want it to be common.

So,

 [anItemTheCombobox.GetType().GetProperty(theCombobox.DisplayMember).GetValue(theCombobox, null)]; 

Background

I used it in a generic function called calculateAndSetWidth. The function checks all the elements in the list to determine maxWidth:

 public void calculateAndSetWidth(ListBox listbox, int minWidth = 0) { Graphics graphics = this.CreateGraphics(); int maxWidth = 0; SizeF mySize = null; foreach (object item in listbox.Items) { mySize = graphics.MeasureString(item.GetType().GetProperty(listbox.DisplayMember).GetValue(item, null), listbox.Font); if (mySize.Width > maxWidth) { maxWidth = mySize.Width; } } listbox.Width = Math.Max(maxWidth, minWidth); 

}

+1
source

All Articles