列表框项目和所选项目

时间:2013-03-04 09:57:20

标签: c# sqlite

我有一个带有ListBox和Database-class的Form。 Form从数据库中调用一个返回字符串的方法。

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

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

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 个答案:

答案 0 :(得分: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
          }
    }
 }

答案 1 :(得分: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;
              }
        }
}