我在Winform應用程序中有一個文本框和列表框。當我在文本框中鍵入一個值(即字符串)我想列表框顯示數據表中的值,當我從列表框中選擇一個特定的值時,它將顯示在文本框中。如何在dataview中顯示列表框中的值
代碼:
private void txtIName_TextChanged(object sender, EventArgs e)
{
string constring = "Data Source=.;Initial Catalog=Test;User Id=sa;[email protected]";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT distinct * FROM Item_Details", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (dt = new DataTable())
{
sda.Fill(dt);
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text);
listBox1.Visible = true;
listBox1.DataSource = dv;
}
}
}
}
}
,但我得到了在列表框輸出像 「的System.Data.DataRow」。通過調試,我可以在dataview'dv'中看到我想要的行。我在這裏錯過了什麼?謝謝。
您必須在看到您的評論之前將'listBox1.DisplayMember'設置爲您希望看到的列名稱 –
我通過Google搜索獲得了輸出。但我不知道如何在列表框中顯示文本框中的值。謝謝你的努力。 –