简体   繁体   中英

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

Steve_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

David_21 David Ratchford Faculty of Archaeology Department of Scientific Archeology

How to go about that since we know list box doesn't take "\\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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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