简体   繁体   中英

Filter on combobox based on another combobox with same table data in c#

I have 2 combo boxes and data grid view I can filter 2 combo boxes separately base on the table but I want to filter them based on 1st combo box. I tried different ways but my second combo box is empty.. nothing happens.. please help me with this.

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

You're looking for filtering like state/city (as an example), correct? So if someone picks a state in the first combo box, then you want a list of cities within that state to populate in the second combo box. Correct?

Apart from the obvious comments about using parametrized queries and Using the SQL connection, have you actually checked the SQL statement directly in a database management program? To me it looks like there is no space in the statement text between

... comboBox1.Text + "'" + "and ([ICPS].[dbo] ...

Apart from that you obviously need to load the second combo from the selected index event on the first combo.

I have tried it everything work fine now..

private void comboBox1_SelectedIndexChanged(object sender, EventArgs 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]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM