2013-06-21 72 views
1

與相同的表數據的另一組合框我有2個組合框和數據網格視圖我可以單獨過濾器2個的組合框立足於表中,但我想基於第一組合框來過濾他們。我嘗試不同的方法,但我的第二個組合框爲空..沒有任何反應..請幫助我。濾波器基於在c#

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

首先,使用參數化查詢。其次,請你提供關於你的餐桌的更多細節。我想提供幫助,但我不太明白你想要完成什麼。越詳細,越好。 –

+0

爲什麼方法fillcombo1項目添加到comboBox2? – HadleyHope

回答

0

您正在尋找過濾狀態/城市(作爲示例),正確嗎?因此,如果有人在第一個組合框中選取一個狀態,那麼你想要的州內城市的名單在第二個組合框中填充。正確?

0

除了有關使用參數化查詢和Using SQL連接,你居然檢查直接在數據庫管理程序中的SQL語句明顯的意見嗎?對我來說,它看起來像有在聲明文本之間沒有空格

... comboBox1.Text + 「'」 + 「與(ICPS] [DBO] ...

除此之外,你顯然需要從選定的索引事件載入第二個組合的第一個組合。

0

我已經嘗試過一切工作的罰款現在..

私人無效comboBox1_SelectedIndexChanged(對象發件人,EventArgs的) {

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