0

我有一个带有 ListBox 和数据库类的表单。表单从数据库中调用一个方法,该方法返回一个字符串。

public listItem()
    {
        InitializeComponent();
        Db = new Database();
        itemList = new List<string>();
        showAllItems();
    }

现在,showAllItems 函数调用 Db.getAllitems() 函数,该函数返回一个列表。

private void showAllItems()
    {
        itemList = Db.getAllItems();
        lb_itemList.DataSource = itemList;
    }

列表显示列表框中所有项目的名称。但是,我也想返回项目的描述。但我不希望描述显示在列表框中。我希望它显示在列表框旁边的标签上,该标签显示所选项目的描述。

我的主要问题是,我不知道如何在不显示列表框中的所有数据的情况下返回多个数据。我只希望列表框中的名称和其他数据显示在标签上的列表框旁边,具体取决于列表框中的所选项目

    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;

    }
4

2 回答 2

1

您可以创建另一个包含您的班级的列表,并在您选择列表框中的项目时从列表中获取项目。

public class YourClass{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
}


public List<YourClass> 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(new YourClass(){
      Id = reader.GetString(reader.GetInt64("Id")),
      Name = reader.GetString(reader.GetOrdinal("name")),
      Description = reader.GetString(reader.GetOrdinal("desc")),
      Price = reader.GetString(reader.GetOrdinal("price"))
});               

    }
}
connection.Close();
return itemList;

}

//in your selectedindexchanged event
private void lb_itemList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (lb_itemList.SelectedIndex > -1)
    {
          var item = lb_itemList.SelectedItem as YouClass;
          if (item != null)
          {
                lblDescription.Text = item.Description;
                lblPrice.Text = item.Price
          }
    }
 }
于 2013-03-04T10:07:30.707 回答
1

像这样创建自定义Class

public class Item 
{
     public string Name;
     public string Description;
}

然后更改您的返回方法:

public List<Item> getAllItems()
{
        List<Item> itemList = new List<Item>();
        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)));
                var item = new Item();
                item.Name = reader.GetString(reader.GetOrdinal("name"));
                item.Description = reader.GetString(reader.GetOrdinal("specific"))
                itemList.Add(item);
            }
        }
        connection.Close();
        return itemList;
}

并将其绑定到您的列表,如下所示:

private void showAllItems()
{
        itemList = Db.getAllItems();
        lb_itemList.DisplayMember = "Name";
        lb_itemList.DataSource = itemList;
}

最后一个实现SelectedIndexChanged事件来处理描述标签:

private void lb_itemList_SelectedIndexChanged(object sender, EventArgs e)
{
        if (lb_itemList.SelectedIndex > -1)
        {
              var item = lb_itemList.SelectedItem as Item;
              if (item != null)
              {
                    lblDescription.Text = item.Description;
              }
        }
}
于 2013-03-04T10:16:19.430 回答