【发布时间】:2020-06-16 07:24:35
【问题描述】:
我正在使用 Visual Studio 中的 Windows 窗体。用 C# 编码
我想帮助我将列表框项添加到文本框。代码如下:
//*当一个按钮被点击时,它会将每个客户的详细信息显示到一个列表框中。每行都有一个客户的名字、姓氏、电话和电子邮件地址。客户的详细信息来自数据库。
string customerFirstname = "";
string customerLastname = "";
string customerPhone = "";
string customerEmail = "";
private void buttonSearch_Click(object sender, EventArgs e)
{
SQL.selectQuery("SELECT * FROM Customer");
if (SQL.read.HasRows)
{
//this goes through each table row in the database
while (SQL.read.Read())
{
//the values are obtained from the database and assigned to the variables.
customerFirstname = SQL.read[0].ToString();
customerLastname = SQL.read[1].ToString();
customerPhone = SQL.read[2].ToString();
customerEmail = SQL.read[3].ToString();
//Each row is displayed in the listbox
listBoxCustomers.Items.Add(
customerFirstname
+ customerLastname + customerPhone + customerEmail);
}
}
}
选择一行后,我想单击一个按钮并在文本框中显示该特定客户的详细信息。
这是我使用的代码:
private void buttonSelectCustomerFromSearch_Click(object sender, EventArgs e) {
textboxCustomerFirstName.Text = customerFirstname;
textboxCustomerLastname.Text = customerLastname;
textboxCustomerPhone.Text = customerPhone;
textboxCustomerEmail.Text = customerEmail; }
我在将所选列表框项中的值添加到文本框时遇到问题。当我运行它时,文本框会显示数据库中最后一个客户的详细信息,它会忽略任何选定的行。你能帮忙吗?谢谢。
【问题讨论】:
-
您使用的是什么 UI 技术:例如 WPF、WinForms 等?
-
欢迎。阅读this 教程以获得更好的方法。
标签: c#