Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于c中具有相同表数据的另一个组合框对组合框进行筛选#_C#_Datagridview_Combobox - Fatal编程技术网

C# 基于c中具有相同表数据的另一个组合框对组合框进行筛选#

C# 基于c中具有相同表数据的另一个组合框对组合框进行筛选#,c#,datagridview,combobox,C#,Datagridview,Combobox,我有两个组合框和数据网格视图,我可以根据表分别过滤两个组合框,但我想根据第一个组合框过滤它们。我尝试了不同的方法,但我的第二个组合框是空的。。什么事也没有发生。。请帮我做这个 { String Query = " SELECT distinct [t_street_name] FROM [ICPS].[dbo].[tickets] "; SqlConnection conDataBase = new SqlConnection(conString); SqlComm

我有两个组合框和数据网格视图,我可以根据表分别过滤两个组合框,但我想根据第一个组合框过滤它们。我尝试了不同的方法,但我的第二个组合框是空的。。什么事也没有发生。。请帮我做这个

{
    String Query = " SELECT  distinct [t_street_name] FROM  [ICPS].[dbo].[tickets]  ";
    SqlConnection conDataBase = new SqlConnection(conString);
    SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
    SqlDataAdapter sda = new SqlDataAdapter(cmdDataBase);
    SqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();

        while (myReader.Read())
        {
            string t_street_name = myReader["t_street_name"].ToString();
            comboBox1.Items.Add(t_street_name);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

void fillcombo1()
{

    String Query =  ("SELECT  distinct [t_zone_name] FROM  [ICPS].[dbo].[tickets] where  [t_street_name] ='" + comboBox1.SelectedItem + "'conString ") ;
    SqlConnection conDataBase = new SqlConnection(conString);
    SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
    SqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();

        while (myReader.Read())
        {
            string t_zone_name = myReader["t_zone_name"].ToString();
            comboBox2.Items.Add(t_zone_name);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection conDatabase = new SqlConnection(constring);

    conDatabase.Open();

    DataTable db = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(String.Format("select  distinct *  from" + " [ICPS].[dbo].[tickets] " +
    "where   [ICPS].[dbo].[tickets].[t_street_name]  = '" + comboBox1.Text + "'" +
    "and ([ICPS].[dbo].[tickets].[t_date_time_issued]) BETWEEN Convert(DATETIME, '{0}', 103) AND Convert(DATETIME, '{1}', 103)", StartDate.Value.ToString("dd/MM/yyyy"), EndDate.Value.ToString("dd/MM/yyyy")), constring);

    sda.Fill(db);

    dataGridView1.DataSource = db;
}

你正在寻找像州/市这样的过滤(作为一个例子),对吗?因此,如果有人在第一个组合框中选择了一个州,那么您希望在第二个组合框中填充该州内的城市列表。正确吗?

除了关于使用参数化查询和SQL连接的明显注释外,您是否确实在数据库管理程序中直接检查了SQL语句?在我看来,语句文本之间似乎没有空格

。。。comboBox1.Text+“'”+ “和([ICPS].[dbo]


除此之外,您显然需要从第一个组合上的选定索引事件加载第二个组合。

我已经尝试过了,现在一切都正常了

私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e) {

         SqlConnection conDatabase = new SqlConnection(constring);

        comboBox2.SelectedIndex = -1;
        comboBox2.Items.Clear();
        if ( conDatabase.State == ConnectionState.Closed)
        {
            conDatabase.Open();
        }
        SqlCommand cmd = new SqlCommand(" select distinct  sz_zone_name from  [ICPS].[dbo].[spid_zones] " + 
            "inner join [ICPS].[dbo].[spid_street]" +
             " on [ICPS].[dbo].[spid_street].ss_number = [ICPS].[dbo].[spid_zones].[sz_street_number]"  +
         " where [ICPS].[dbo].[spid_street].ss_name  ='" + comboBox1.SelectedItem + "'", conDatabase);
        SqlDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            comboBox2.Items.Add(rd[0]);
        }

首先,使用参数化查询。第二,请提供有关表的更多详细信息。我想提供帮助,但我不太清楚您具体想实现什么。详细信息越多越好。为什么方法fillcombo1会向Combox2添加项?