首页 文章

列表框项目和所选项目

提问于
浏览
0

我有一个带有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 回答

  • 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

    像这样创建自定义 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;
                  }
            }
    }
    

相关问题