Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the DisplayMember value in the DrawItem event of a comboBox? C#

Tags:

c#

winforms

Please consider 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 DrawItem event, I want to get the value of the DisplayMember (name) of the comboBox and assign it to some variable. So far, I got this code and it seems not to work... Please have it corrected. Thanks in advance....

void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    string name = ((System.Data.DataRowView)(comboBox1.SelectedValue = e.Index))["name"].ToString();

    // do something
    //  
}
like image 876
yonan2236 Avatar asked Aug 05 '11 10:08

yonan2236


3 Answers

Ok, i just came across 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);

Per MSDN:

If the DisplayMember property is not specified, the value returned by GetItemText is the value of the item's ToString method. Otherwise, the method returns the string value of the member 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

like image 107
Kevin McCormick Avatar answered Oct 25 '22 05:10

Kevin McCormick


how about using just combobox item, it is selected displayed value:

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

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

EDIT:

Ok I understood whats wrong. I tested with strings as datasource, so in your code should work this:

string name = ((DataRowView)comboBox1.Items[e.Index])["name"];
like image 37
Renatas M. Avatar answered Oct 25 '22 05:10

Renatas M.


If you want to make a really generic function you could elaborate a bit:

Lets say you have two comboboxes, one containing items based of custom collection A with DisplayMember AA and the other contains items based on custom collection B with DisplayMember BB:

How could a generic function know which value to return? Based of DisplayMember of course, but you don't want to pass AA / BB to a generic function if you want it generic.

So

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

Background

I used it in a generic function called calculateAndSetWidth. The function loops through all items in listbox to determine the 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);

}

like image 1
Doctor Rudolf Avatar answered Oct 25 '22 05:10

Doctor Rudolf