iT邦幫忙

0

WindowsForm連動SQLite製作雙層選單

  • 分享至 

  • xImage

目前狀況:
選擇combobox1時,combobox2會自動帶出對應的下拉式選單,
當選擇combobox2下拉式選單內的資料時,資料不能寫在combobox2的格子內。

combobox1 = Power_Mode_Box
combobox2 = Power_Mode_Brand_Box

以下是程式碼:
switch(Power_Mode_Box.Text)
{
case "SSD":
sqlite_connect = new SQLiteConnection("Data source=E.db");//建立資料庫連線
sqlite_connect.Open();// Open
sqlite_cmd = sqlite_connect.CreateCommand();//create command
sqlite_cmd.CommandText = "SELECT SSDxBrand FROM SSDxBrand"; //select table
SQLiteDataReader sqlite_datareader1 = sqlite_cmd.ExecuteReader();
while (sqlite_datareader1.Read()) //read every data
{
String Brand1_load = sqlite_datareader1["SSDxBrand"].ToString();
Power_Mode_Brand_Box.Items.Add(Brand1_load);
}
sqlite_connect.Close();
MessageBox.Show("已完成");
break;
}


問題補充1:
我想做兩層下拉式選單,當combobox1選擇SSD時,combobox2的下拉式選單會自動塞入SQLite對應的資料,目前已完成到這邊。

但是我去點選combobox2的下拉式選單,選單展開後,假設有a、b、c 三筆資料,我都無法從下拉式選單選擇其中一筆寫到combobox2的格子內。


問題補充2:
我該如何清除items,但不清除text呢?


已解決...
我把Combobox2重新刪掉再建一次就好了...
謝謝大家!

不是很能理解問題敘述
看程式碼你是選擇combobox1 新增 combobox2選項
那問題是甚麼?
想要選擇combobox2時不新增combobox2?
目前兩個event共用?
還是有無錯誤訊息
請麻煩將情境資訊補完整一點
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-01-15 02:57:27
最佳解答

問題補充2:
我該如何清除items,但不清除text呢?

Power_Mode_Brand_Box.Items.Clear();

WindowsForm連動SQLite製作雙層選單的完整範例程式碼:

using System;
using System.Windows.Forms;
using System.Data.SQLite;

namespace WindowsFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 宣告SQLite連線
        SQLiteConnection sqlite_connect; 
        
        // 宣告SQLite指令
        SQLiteCommand sqlite_cmd; 
        
        private void Form1_Load(object sender, EventArgs e)
        {   
            // 在Form的Load事件中,加入combobox2的SelectedValueChanged事件
            Power_Mode_Brand_Box.SelectedValueChanged += new EventHandler(Power_Mode_Brand_Box_SelectedValueChanged);                        
        }

        //當使用者選擇combobox2的選項時,就會觸發SelectedValueChanged事件
        private void Power_Mode_Box_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 透過switch來判斷使用者選擇的Power_Mode_Box是哪一種
            switch (Power_Mode_Box.Text)
            {
                case "SSD":
                    // 當使用者選擇SSD時,清除combobox2 的items
                    Power_Mode_Brand_Box.Items.Clear(); 
                    
                    //建立資料庫連線
                    sqlite_connect = new SQLiteConnection("Data source=E.db"); 
                    
                    // Open
                    sqlite_connect.Open(); 
                    
                    //create command
                    sqlite_cmd = sqlite_connect.CreateCommand(); 
                    
                    //select table
                    sqlite_cmd.CommandText = "SELECT SSDxBrand FROM SSDxBrand"; 
                    
                    SQLiteDataReader sqlite_datareader1 = sqlite_cmd.ExecuteReader();

                    //read every data
                    while (sqlite_datareader1.Read()) 
                    {
                        String Brand1_load = sqlite_datareader1["SSDxBrand"].ToString();
                        Power_Mode_Brand_Box.Items.Add(Brand1_load);
                    }
                    sqlite_connect.Close();
                    MessageBox.Show("已完成");
                    break;
            }
        }

        // combobox2的SelectedValueChanged事件,將combobox2.SelectedItem.ToString()的值寫入combobox2.Text屬性中
        private void Power_Mode_Brand_Box_SelectedValueChanged(object sender, EventArgs e)
        {       
            //將選擇的選項值顯示在combobox2中
            Power_Mode_Brand_Box.Text = Power_Mode_Brand_Box.SelectedItem.ToString();            
        }
    }
}

0
Juro十六
iT邦新手 3 級 ‧ 2023-01-14 18:12:21

手上沒電腦給個概念
Visual studio在開發WinForm的時候將你的combobox觸發的地方有個被選擇後觸發
再來的code就是當combobox1被選擇後new一個List
裡面的Item撈你的SQLite找分類為combobox1.text
用mssql的cmd大概是

select * from TABLE_1 where equipment = 'SSD'
string equipment = combobox1.text;
string SQLcommand = "select * from TABLE_1 where equipment = '"+equipment+"'";

再來讓combobox2.Item = 當時新增的list的item
大概這樣就能解決你的問題

我要發表回答

立即登入回答