【问题标题】:Categories from 2 ComboBox affecting third Combobox for datagridview with SQL来自 2 个 ComboBox 的类别影响使用 SQL 的 datagridview 的第三个 Combobox
【发布时间】:2014-01-13 18:45:15
【问题描述】:

把它写成短篇小说

我想要类似 "SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox" 之类的东西,我该如何进行 sql 查询?

=========================== 长篇大论

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

有点,我一直在试图弄清楚如何让第三个组合框值由第一个和第二个决定。

我想要的是,获取主类别和子类别的值以影响第三个组合框的列表。

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

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

像这样:

对于 Main Category 和 Sub-Category ,我让它使用此代码。 此代码还包括第 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

dbo.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();

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

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

已回答 - 创建了一个名为 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;
        }

【问题讨论】:

  • 真的有人能帮忙吗?

标签: c# sql search datagridview combobox


【解决方案1】:

尝试在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;
}

【讨论】:

  • @MohdNasrulIwanFajaruddin 检查一下,如果有任何错误,请告诉我。
  • 我已经添加了我的答案..它在我上面编辑的帖子中..在下面..但我已经尝试了你的代码..在这里和那里改变了一些部分,它起作用了。 . :)
  • @MohdNasrulIwanFajaruddin 好的,如果您认为我建议的解决方案值得解决您的问题,那么您必须将我建议的解决方案标记为答案。
【解决方案2】:

您必须通过以下查询使用:

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

【讨论】:

  • thx.. 但我使用了字符串 myRequest = "SELECT cName FROM ComDet where mainCate = '" + mainCatU.SelectedItem.ToString() + "' and Subcat = '" + subCatU.SelectedItem.ToString( ) + "'";反而。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多