如何使列表框中的数据逐行显示,因为列表框不需要"n"?



我只是尝试在列表框中逐行从数据库中读取数据。 这是我的每一列部分,"读者。GetString"代表 - 用户名,姓名,姓氏,部门,教师等。我希望他们都 对于每一行逐行显示,而不是与"+="并排显示 例;:

Steve_J

史蒂夫·杰克逊

浪漫-日耳曼系

德国语言文学

当前编写的代码块的输出(都在同一行中,每列之间有空格(是:

Steve_J 史蒂夫杰克逊 罗曼斯-日耳曼学院 德国语言文学系

David_21 大卫·拉奇福德考古学院科学考古系

既然我们知道列表框不需要"",该怎么办?

源代码:

private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection connect = new SqlConnection())
{
connect.ConnectionString = @"Data Source=DESKTOP-9I0BNASSQLEXPRESS;Initial Catalog=CAVID;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
connect.Open();
using (SqlCommand command = new SqlCommand("select *  from Istifadeciler", connect))
{
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{                        
string data = reader.GetString(0)+ " ";
data += reader.GetString(1) + "  ";
data += reader.GetString(2) + "  ";
data += reader.GetString(3) + "  ";
data += reader.GetString(4)+ "  ";
data += reader.GetString(5) + "  ";
data += reader.GetInt32(6).ToString()+ "  ";
data += reader.GetInt32(7).ToString()+ "  ";
listBox1.Items.Add(data);                        
}
}
}

正如Magisch所建议的那样...

using System.Collections.Generic;
using System.Windows.Forms;
namespace _51189773
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ListBox lb = new ListBox();
lb.Items.Add("result 1");
lb.Items.Add("result 2");
lb.Items.Add("result 3");
Controls.Add(lb);
}
}
}

最新更新