来自2 ComboBox的类别影响第三个Combobox for datagridview with SQL

把它写成短篇小说

我想要“SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox”,我该怎么做sql查询?

==========================长话

我已经创建了一个带有编码的表单,但我需要额外的帮助。

有点,我坚持试图弄清楚如何让第三个combobox值由第一个和第二个决定。

我想要的是,获得主类别和子类别的值以实现第三个combobox的列表。

我只需要SQL查询,例如:“SELECT companyName FROM table where maincategory = firstcombobox and subcategory = secondcombobox”

然后在主要和子类别的选择中显示公司名称。

像这样 :

主要表格

对于主类别和子类别,我使用此代码。 此代码还包括第3个ComboBox代码,该代码现在在没有第一个和第二个combobox连接到第3个combobox代码的情况下运行。

public partial class User : Form { Dictionary<string, List> Category = new Dictionary<string, List>(); DataSet ds1; public User() { InitializeComponent(); } private void User_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; conn.Open(); SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn); ds1 = new DataSet(); daMain.Fill(ds1, "Maincate"); DataTable dt = ds1.Tables["MAINCATE"]; foreach (DataRow dr in dt.Rows) { List SubCats = new List { dr["Subcat1"].ToString(), dr["Subcat2"].ToString(), dr["Subcat3"].ToString(), dr["Subcat4"].ToString() }; Category.Add(dr["mainCate"].ToString(), SubCats); mainCatU.Items.Add(dr["mainCate"].ToString()); } mainCatU.DropDownStyle = ComboBoxStyle.DropDownList; mainCatU.Enabled = true; subCatU.DropDownStyle = ComboBoxStyle.DropDownList; //**Code for third combobox** SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn); DataTable dt1 = new DataTable(); ListU.DataSource = dt1; daSearch.Fill(dt1); ListU.ValueMember = "cName"; ListU.DisplayMember = "cName"; ListU.DropDownStyle = ComboBoxStyle.DropDownList; ListU.Enabled = true; //**----------------------** conn.Close(); } private void mainCatU_SelectedIndexChanged(object sender, EventArgs e) { if(Category.ContainsKey(mainCatU.SelectedItem.ToString())) { subCatU.DataSource = Category[mainCatU.SelectedItem.ToString()]; } } 

数据库如下所示:

dbo.MAINCATE MAINCATE

dbo.ComDet ComDet

并且View Selected Company button的代码是:

 private void searchBtn_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; conn.Open(); SqlDataAdapter daS = new SqlDataAdapter("select cName, cDetails, cDetails2 from ComDet where cName = @cName", conn); daS.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue; DataTable dts3 = new DataTable(); daS.Fill(dts3); dataGridView1.DataSource = dts3.DefaultView; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; conn.Close(); } 
  • 再次,现在第三个combobox被编码为在没有maincategory和子类别的情况下运行

========================

已回答 – 创建了一个名为search的按钮,并将表单加载的代码转换为按钮,添加了SQLCon,并添加了所需的SQLQuery。

大家好.. 🙂

 private void button2_Click_1(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; conn.Open(); string myRequest = "SELECT cName FROM ComDet where mainCate = '" + mainCatU.SelectedItem.ToString() + "' and Subcat = '" + subCatU.SelectedItem.ToString() + "'"; SqlDataAdapter daSearch = new SqlDataAdapter(myRequest, conn); DataTable dtSea = new DataTable(); ListU.DataSource = dtSea; daSearch.Fill(dtSea); ListU.ValueMember = "cName"; ListU.DisplayMember = "cName"; ListU.DropDownStyle = ComboBoxStyle.DropDownList; ListU.Enabled = true; } 

尝试在mainCatUsubCatU组合的SelectedValue更改事件上调用以下函数:

 private void SetCompanyList() { if (string.IsNullOrEmpty(Convert.ToString(mainCatU.SelectedValue)) || string.IsNullOrEmpty(Convert.ToString(subCatU.SelectedValue))) return; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; conn.Open(); SqlDataAdapter daMain = new SqlDataAdapter("SELECT cName FROM ComDet where mainCate = @mainCat subCat = @subCate", conn); daMain.SelectCommand.Parameters.Add("@mainCat", SqlDbType.VarChar).Value = mainCatU.SelectedValue; daMain.SelectCommand.Parameters.Add("@subCate", SqlDbType.VarChar).Value = subCatU.SelectedValue; DataTable _table = new DataTable(); daMain.Fill(_table); ListU.DataSource = _table; } 

您必须使用以下查询:

  SELECT companyName FROM table where mainCategory = '" + mainCatU.selectedValue + "' and subcategory = '" + subCatU.selectedValue + "'"