简体   繁体   English

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

[英]How to make data in the listbox appear line-by-line since listbox doesn't take “\n”?

I just try to read data from database line by line inside list box. 我只是尝试在列表框中逐行从数据库读取数据。 Here is my each column section that "reader.GetString" represents - Username, Name, Surname, Department, Faculty etc.. I want them all to appear line by line for every row not side by side with "+=" for example;: 这是我的“ reader.GetString”所代表的每一列部分-用户名,名称,姓氏,系,学院等。我希望它们全部针对每一行而不是与“ + =”并排出现。 :

Steve_J 史蒂夫·J

Steve Jackson 史蒂夫·杰克逊

Romance-Germanic Department 浪漫德语部

German language and Literature 德国语言文学

and the output of currently written code blocks (all in the same line with spaces between each column) is : 当前编写的代码块的输出(均在同一行中,每列之间都有空格)为:

Steve_J Steve Jackson Romance-Germanic faculty Department of German language and Literature Steve_J史蒂夫·杰克逊(Steve Jackson)罗曼史德语德语文学系

David_21 David Ratchford Faculty of Archaeology Department of Scientific Archeology David_21 David Ratchford科学考古系考古学系

How to go about that since we know list box doesn't take "\\n"? 由于我们知道列表框不带“ \\ n”,该如何处理?

Source Code: 源代码:

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

As suggested by Magisch... 正如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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM