将列表框设置为2个值而不是1个值

本文关键字:1个值 2个 列表 设置 | 更新日期: 2023-09-27 17:49:48

我正在尝试格式化一个文本框,以便当它从数据库读取时,两个文本值出现在列表框的同一行上,并显示为1值

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." ;
}

我把//是我卡住的地方,我想要的名字和姓氏出现在每个旁边。感谢您的帮助

将列表框设置为2个值而不是1个值

这是局部变量的一个很好的用例:

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));

应该可以了

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