views:

2266

answers:

3

I have a combobox, but I'd like it to show two different column values from the datasource.

So instead of simply filling it with "rowid" (possible values "1","2","3" etc) I'd like it to show "letter" ("a","b","c") column as well, but in that same box.

The actual value that it sends can just that rowid value, but for user friendly purposes, I need both.

this.comboBox1.DataSource = this.tblIDSourceBindingSource;
this.comboBox1.DisplayMember = "rowid";
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Name = "comboBox1";
this.comboBox1.ValueMember = "rowid";

So as you can see, it's displaying "rowid". Trying to tell it to display "letter" in addition throws everything off.

+1  A: 

Assumption: you can't just display "letter" and be done with it.

change your proc to so that instead of

select rowid, letter, blah
from table

do something like

select rowid + ' ' + letter as display, rowid, letter, blah
from table

Or if you don't like the space, use the delimiter(s) of your choice (pipe, slash, wrap rowid in parenthesis, etc).

then use "display" as your DisplayMember.

p.s. if rowid is an int, you need to use CAST or CONVERT to make it an nvarchar or varchar.

p.p.s. if you can't change your proc, then the long way is to create a light entity that is the same as your return, but add a read only property called Display whose get is something like

return string.Format("{0}{1}{2}", this.rowid, delimiterOfChoice, this.letter);

or something of that nature.

then just use a collection of those entities as your bindingsource instead.

Andy_Vulhop
why the downvote? this seems like a perfectly viable solution. please comment if you downvote someone's answer
Andy_Vulhop
Drive by downvote. I voted it up.
scrot
Perhaps someone downvoting others to get their own answer up top.
scrot
@tim seems the likely case to me too
Andy_Vulhop
A: 

As a quick and dirty hack, you should be able to add a new column to your datatable once it is populated from the database. Loop through the table building the string you want to display for each row and stick it into your new column. Then bind it to display this value instead.

eviltobz
why not just select letter + ' ' + rowid ? or letter + '/' + rowid? There's no need to have that column since it is not relevant to the data itself. (did not downvote btw)
Andy_Vulhop
Nor did I. As I said, it was probably the top vote getter's doing.
scrot
I did down vote - well the opening line was "quick and dirty hack" shouhld have been enough. You would add so many problems, need to maintain the new db column, why not use the tools that are there? Also Datasource does not always = Database
Adrian
There is no new database column, just a new column on the datatable in the code before the data is bound. The OP said that he wasn't able to modify anything in the database so changing the select statement wasn't possible.
eviltobz
+2  A: 

you need to overide the Format event

    // FormatString property is shown in the designer, so you can provide
    // some designer support. If you are not using the desinger then you
    // either set it in the form/control ctor, or use a string directly
    // in the Format event.
    ListControl.FormatString = "{0}/{1}" ;

    private void listBoxCategory_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = string.Format(((ListBox)sender).FormatString,
          ((ClassOfListItem)e.ListItem).LongName,
          ((ClassOfListItem)e.ListItem).Key);
    }
Adrian
+1, short and to the point.
Henk Holterman
should "{0}/{2}" be "{0}/{1}"?
Andy_Vulhop
doh! I'll edit it
Adrian