7

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?

2
  • Are you using WinForms or WPF? Commented Jun 23, 2010 at 0:45
  • in which format you have your data .. ?? Commented Jun 23, 2010 at 2:00

4 Answers 4

18

First, you can leave your DisplayMember with one property, let's say:

ListBox1.DisplayMember = "team1";

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:

private void ListBox1Format(object sender, ListControlConvertEventArgs e)
{

}

And now write these following lines inside:

private void ListBox1Format(object sender, ListControlConvertEventArgs e)
{
    // Assuming your class called Scores
    string team1 = ((Scores)e.ListItem).team1.ToString();
    string team2 = ((Scores)e.ListItem).team2.ToString();
    string battingteam = ((Scores)e.ListItem).battingteam.ToString();
    string score = ((Scores)e.ListItem).score.ToString();

    e.Value = team1 + " vs " + team2 + ": " + battingteam +" Score: " + score;
}

That's it ;)

Sign up to request clarification or add additional context in comments.

Comments

1

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 create List<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

List<string> teams = new List<string>();
foreach (DataRow dataRow in teamsDataTable.Rows)
{
    teams.Add(dataRow["team1"].ToString() + " vs " + dataRow["team2"].ToString() + ": " + dataRow["battingteam"].ToString() + " Score: " + dataRow["score"].ToString());
}
listBox1.DataSource = teams;

3 Comments

The data is in DataSet that contains many DataTables. Each dataTable has a tableAdapter that i use to get and fill data
I am going to try that method
you can use the above method but by replacing teamsDataTable.Rows by dataset.Table[X].Rows
1

I was also trying to do something similar and found with a combination of sql and display member, it was very simple. For example

string sql = "SELECT team1+ 'vs' + team2+ ':' +battingteam + 'Score:' + score";
sql += "AS newColumnNametbl from table";
ListBox lst = new ListBox();
lst.DataSource = ds; //datasource created
lst.DisplayMember = "newColumnNametbl";

I found it easier to just manipulate the sql then writing lots of line of code. Hope it helps in terms of easier coding.

Comments

0

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:

    MyListBox.DrawMode = DrawMode.Normal;
    List<KeyValuePair<string, string>> KeyValueList = new List<KeyValuePair<string, string>>();
                foreach (Student Curritem in StudentsList)
                {
                    KeyValueList.Add(
                        new KeyValuePair<string, string>(Curritem.FirstName + " Age: <" + Curritem.Age + ">",
                                        Curritem.ID.ToString()));
                }
                MyListBox.DisplayMember = "Key";
                MyListBox.ValueMember = "Value";
                MyListBox.DataSource = KeyValueList; 
                MyListBox.Refresh();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.