userV
userV

Reputation: 11

Formatting a listbox to have 2 values rather than 1

I am trying to format a textbox so that when it reads from a database, two text values appear on the the same line in the listbox and appear as 1 value

while (reader.Read())
{
    listBox1.Items.Add(reader["FirstName"].ToString());
    listBox1.Items.Add(reader["Surname"].ToString());
    //listBox1.Items.Add(string.Format("{0} | {1}", ));
    label2.Visible = true;
    label2.Text = "So far, " + listBox1.Items.Count.ToString() + " people have applied for the " + comboBox1.SelectedItem.ToString() + " \ncourse." ;
}

Where I have put the // is where I am getting stuck, I want the FirstName and Surname to appear next to each. Thanks for any help

Upvotes: 0

Views: 55

Answers (2)

Abdul Khan
Abdul Khan

Reputation: 363

This should work for you

 listBox1.Items.Add(string.Format("{0} | {1}",reader["FirstName"].ToString(),reader["Surname"].ToString()));

Upvotes: 0

D Stanley
D Stanley

Reputation: 152556

This is a good use case for local variables:

var firstName = reader["FirstName"].ToString();
var lastname = reader["Surname"].ToString();
//listBox1.Items.Add(firstname);
//listBox1.Items.Add(lastname);
listBox1.Items.Add(string.Format("{0} | {1}", firstname, lastname));

Upvotes: 1

Related Questions