如何从组合框中显示复选列表框中的项目

本文关键字:列表 项目 显示 组合 | 更新日期: 2023-09-27 18:13:16

我有一个comboBox和一个checkListBox在我的windows窗体应用程序连接到我的SQL数据库。我得到了绑定数据部分的工作,但我不确定如何显示数据在checkListBox时,选择了comboBox项目。假设我的组合框中有10个项目与我的SQL数据库绑定,它们在列名("应用程序名称")下,如excel, word, android, eclipse等....我在表单开始加载时调用这个方法。抱歉,代码太长了。

这是applicationComboBox

的代码
private void loadComboBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
            try
            {
                myConn.Open();
                //my table name is Application_Detail
                string query = "select * from Application_Detail";
                myCommand = new SqlCommand(query, myConn);
                //reading the value from the query
                SqlDataReader dr = myCommand.ExecuteReader();
                //Reading all the value one by one
                while (dr.Read())
                {
                    //column is 1 in Application_Detail Data
                   //GetString(1) display the 2nd column of the table
                    string name = dr.GetString(1);
                    //display the application name in column 2 - 
                    applicationComboBox.Items.Add(name);
                }
                myConn.Close();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

这部分代码的结果是:

 //label Name        //Application Name
  Application Name:                    
                      Excel
                      Word
                      NotePad
                      PowerPoint
                      SubLime
                      Eclipse
在我调用这个方法之后,我想要显示老师的名字,这是根据用户在这个applicationComboBox中选择的。因此,如果教师1,2,3使用Excel,并且用户从组合框中选择了Excel,则checkListBox将显示教师1,2,3,反之亦然。为此,我在comboBox1_SelectedIndexChanged方法中调用该方法,因为我想在从组合框中选择项时显示详细信息。下面是我的代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
          //I check if the comboBox index is at 0, it disable the button. 
            if (applicationComboBox.SelectedIndex == 0)
            {
                exportButton.Enabled = false;
                this.teacherCheckListBox.DataSource = null;
                teacherCheckListBox.Items.Clear();
            }
          //it it is not at 0, 
            else
            {
                exportButton.Enabled = true;
                 //call this method
                fill_checkListBox();
            }
            //teacherCheckListBox
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
    private void fill_checkListBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
            try
            {
                myConn.Open();
                //for reading purpose, I break down by long statement 
                //In this statement, I left join 3 table (Teacher_Detail,  AppUser_Detail, and Application_Detail table). My AppUser_Detail contains all 3 id (teacherId, applicationId, and AppUserId). I then set filter the table using `where` keyWord to make the applicationId = the comboBox text 
                string query = "SELECT
                                  td.chineseName,
                                  ad.applicationId,
                                  aud.applicationId,
                                  ad.applicationName 
                              FROM[AppUser_Detail] as aud 
                              LEFT JOIN[Teacher_Detail] as td 
                              ON aud.teacherId = td.teacherId
                              LEFT JOIN[Application_Detail] as ad 
                              ON aud.applicationId = ad.applicationId
                              where aud.applicationId = '" + applicationComboBox.Text + "' AND NOT(td.teacherId IS NULL)
";
                myCommand = new SqlCommand(query, myConn);

                //reading the value from the query
                SqlDataReader dr = myCommand.ExecuteReader();
                //Reading all the value one by one
                while (dr.Read())
                {
                    //column is 0 where the teacherName belong in my Teacher_Detail table
                    string name = dr.GetString(0);
                     //I tried to set the text of the checkListBox as the teacherName, but I can't somehow
                    teacherCheckListBox.Text = name;
                }
                myConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

当我像这样运行程序时,它说Conversion failed when converting the varchar value "Excel" to data type int.有办法修复它吗?这不应该是一个问题,因为在我的Application_Detail表中,我的applicationName和我的teacherName的数据类型设置为nvarchar(50)和applicationId和teacherId = int;

如何从组合框中显示复选列表框中的项目

问题是这一行,我认为:

 where aud.applicationId = '" + applicationComboBox.Text +

根据你的代码,我认为applicationId是一个int和applicationComboBox。文本就是文本。

试试这个:

where ad.applicationName = '" + applicationComboBox.Text.Trim() +

试试这个:

if (string.IsNullOrWhiteSpace(teacherCheckListBox.FindString(name))
{
    teacherCheckListBox.Items.Add(name);
}