omboBox影响第三个Combobox的datagridvie

omboBox影响第三个Combobox的datagridvie

本文介绍了类别2 ComboBox影响第三个Combobox的datagridview与SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的东西像SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox,我如何做sql查询?

i wanted something like "SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox" , how do i do the sql query?

========================
Long story

==========================Long story

我创建了一个具有工作代码的表单,但我需要额外的帮助。

I have created a form , with a working coding , but i need an extra assistance.

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

Somewhat , i am stuck on trying to figure out how to let the 3rd combobox value , determined by the first and second.

和我想要的是,类似的东西,获得Main

and what i wanted is , something like , getting the value of Main Category and Sub category to effect the list of the third combo box.

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

i just need the SQL query , such as : "SELECT companyName FROM table where maincategory = firstcombobox and subcategory = secondcombobox"

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

and then shows the company name within the picks of main and sub category.

对于主类别和子类别,我使用这个代码。
此代码还包括第三个ComboBox代码,现在无需将第一个和第二个组合框附加到第三个组合框代码即可运行。

For the Main Category and Sub-Category , i have it working with this code.This code also includes the 3rd ComboBox code which right now operates without the first and second combobox attached to the 3rd combobox code.

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

dbo.MAINCATE

dbo.ComDet

dbo.ComDet

,而查看所选公司 按钮的代码为:

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 required ..

    Answered - Created a button called search , and took the coding from form load into the button , added with SQLCon , and added the SQLQuery needed..

    Thx guys ..:)

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


    推荐答案

    code> SelectedValue 更改事件mainCatU subCatU 组合:

    Try calling following function on SelectedValue change event of mainCatU and subCatU combo:

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

    这篇关于类别2 ComboBox影响第三个Combobox的datagridview与SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:03