来自2 ComboBox的类别影响第三个Combobox用于带有SQL的datagridview

时间:2013-12-26 04:48:03

标签: c# sql search datagridview combobox

把它写成短篇小说

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

========================== 长篇故事

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

有点,我坚持试图找出如何让第一个和第二个确定的第三个组合框值。

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

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

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

像这样:

Main Form

对于主类别和子类别,我使用此代码。 此代码还包括第3个ComboBox代码,该代码现在没有第一个和第二个组合框连接到第3个组合框代码。

public partial class User : Form
    {

        Dictionary<string, List<string>> Category = new Dictionary<string, List<string>>();

        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<string> SubCats = new List<string> 
                {
                 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();

        }
  • 再次,现在第三个组合框被编码为在没有maincategory和子类别的情况下运行

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

已回复 - 创建了一个名为search的按钮,并将表单加载中的编码转换为按钮,添加了SQLCon,并添加了所需的SQLQuery ..

Thx guys .. :))

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;
        }

2 个答案:

答案 0 :(得分:1)

尝试在SelectedValuemainCatU组合的subCatU更改事件中调用以下函数:

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;
}

答案 1 :(得分:0)

您必须使用以下查询:

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