[英]How to make data in the listbox appear line-by-line since listbox doesn't take “\n”?
我只是嘗試在列表框中逐行從數據庫讀取數據。 這是我的“ reader.GetString”所代表的每一列部分-用戶名,名稱,姓氏,系,學院等。我希望它們全部針對每一行而不是與“ + =”並排出現。 :
史蒂夫·J
史蒂夫·傑克遜
浪漫德語部
德國語言文學
當前編寫的代碼塊的輸出(均在同一行中,每列之間都有空格)為:
Steve_J史蒂夫·傑克遜(Steve Jackson)羅曼史德語德語文學系
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);
}
}
}
正如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.