I am using VS2010 for c#. I have some data there in a table
Scores(team1,team2,battingteam,bowlingteam,score) I am using bindingSource to bind it with my listBox. The problem is that i can show only one member in DisplayMember property of listbox (e.g. team1 or team2 only). I want to do something like
team1 + " vs " + team2 + ": " + battingteam +" Score: " + score
how to do that?
I think the best solution for that in C# level is to have agent of KeyValuePair, add your multiple value to the key or to the value then assign it to the listbox like the following: Suppose I have List of Student Class, which is StudentsList, then:
First, you can leave your DisplayMember with one property, let's say:
Now, go to your form in a [Design] mode, right click on the ListBox -> Properties.
In the top of the Properties window, click on Events (lightning icon),
look for Format in the events list below (under Property Changed) and type there some event name, let's say: ListBox1Format , and press Enter. You will see this:
And now write these following lines inside:
That's it ;)
I was also trying to do something similar and found with a combination of sql and display member, it was very simple. For example
I found it easier to just manipulate the sql then writing lots of line of code. Hope it helps in terms of easier coding.
I don't know in which data type you have your data ... but for example if you have them in
DataTable
returned from db you can createList<string>
object and iterate datatable rows and create items in list object with formatting data as you want and by end bind list object to your listbox directly as below