我有一个带有ListBox和Database-class的Form . Form从Database调用一个返回字符串的方法 .
public listItem()
{
InitializeComponent();
Db = new Database();
itemList = new List<string>();
showAllItems();
}
现在,showAllItems-function调用返回List的Db.getAllitems()函数 .
private void showAllItems()
{
itemList = Db.getAllItems();
lb_itemList.DataSource = itemList;
}
列表显示列表框中所有项目的名称 . 但是,我还想返回项目的描述 . 但我不希望描述显示在列表框中 . 我希望它显示在listBox旁边的标签上,该标签显示所选项目的描述 .
我的主要问题是,我不知道如何在不显示列表框中的所有数据的情况下返回多个数据 . 我只想要列表框中的名称和标签上列表框旁边显示的其他数据,这取决于ListBox中的选定项目
public List<string> getAllItems()
{
List<string> itemList = new List<string>();
SQLiteConnection connection = new SQLiteConnection("Data Source=HCI.db");
connection.Open();
SQLiteCommand cmd = new SQLiteCommand("SELECT rid, name, category, price, status, specific FROM items", connection);
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//itemList.Add(reader.GetString(reader.GetInt64(rid)));
itemList.Add(reader.GetString(reader.GetOrdinal("name")));
itemList.Add(reader.GetString(reader.GetOrdinal("category")));
itemList.Add(reader.GetString(reader.GetOrdinal("price")));
itemList.Add(reader.GetString(reader.GetOrdinal("status")));
itemList.Add(reader.GetString(reader.GetOrdinal("specific")));
}
}
connection.Close();
return itemList;
}
2 回答
您可以创建包含类的另一个列表,并在列表框中选择项目时从列表中获取项目 .
像这样创建自定义
Class
:然后改变你的退货方法:
并将其绑定到您的列表:
并最后实现
SelectedIndexChanged
事件来处理描述标签: