由于列表框不使用“ \ n”,如何使列表框中的数据逐行显示?

时间:2018-07-05 11:07:33

标签: c# .net winforms listboxitems

我只是尝试在列表框中逐行从数据库读取数据。 这是我的“ reader.GetString”代表的每个列部分- 用户名,名称,姓氏,系,学院等。我希望他们都 每行显示一行一行,而不与“ + =”并排显示 例如;

  

Steve_J

     

史蒂夫·杰克逊

     

浪漫德语系

     

德语语言文学

     

和当前写入的代码块的输出(均在同一行中,每列之间都有空格)为:

     

Steve_J史蒂夫·杰克逊浪漫德语系德语和文学

     

David_21 David Ratchford科学考古系考古学院

既然知道列表框不带“ \ n”,该如何处理?

源代码:

private void button2_Click(object sender, EventArgs e)
{
    using (SqlConnection connect = new SqlConnection())
    {
        connect.ConnectionString = @"Data Source=DESKTOP-9I0BNAS\SQLEXPRESS;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);                        
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

根据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);
        }
    }
}