ComboBox2依赖于ComboBox1选择的项目,而datagridview1依赖于ComboBox2选择的项目C#windows application

时间:2016-03-20 20:59:36

标签: facebook-c#-sdk

`SqlConnection con = new SqlConnection(“Data Source = DESKTOP-NGRLFB4 \ SQLEXPRESS; Initial Catalog = dboo; Persist Security Info = True; User ID = sa; Password = *****”);         SqlCommand命令;         SqlDataAdapter适配器;         DataTable表;         DataRow博士;         private void Form1_Load(object sender,EventArgs e)         {             string query =“SELECT Going_Id,LeavingFrom FROM LeavingAndGoing”;             comboBox1.DataSource = getData(query);             comboBox1.DisplayMember =“LeavingFrom”;             comboBox1.ValueMember =“Going_Id”;

    }

     public DataTable getData(string query)       
     {           
         command = new SqlCommand(query, con);  
         adapter = new SqlDataAdapter(command);     
         table = new DataTable();        
         adapter.Fill(table);     
         return table;  
     }

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
     {
         int val;          
         Int32.TryParse(comboBox1.SelectedValue.ToString(), out val);
         string query = "SELECT ID, GoingPlace, Id_Gonig FROM GoingTable Where Id_Gonig =" + val;
         comboBox2.DisplayMember = "GoingPlace";
         comboBox2.ValueMember = "Id_Gonig";
         comboBox1.DataSource = getData(query); 

     }
     private bool button1_Clicked = true;


     private void button1_Click(object sender, EventArgs e)
     {
         try
         {

             if (comboBox2.SelectedIndex < 0)
             {
                 MessageBox.Show("Please Enter the Place Where You Are Going");
                 button1_Clicked = false;
             }

             else
             {

                 int val;
                 Int32.TryParse(comboBox2.SelectedValue.ToString(), out val);
                 string query1 = "SELECT ID, Departing_Time, STARTING_COUNTER, END_COUNTER, COACH_TYPE, FARE, ARRIVAL_TIME FROM Seat_Information Where SI_ID = " + val;
                 dataGridView1.DataSource = getData(query1);
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }`

当我尝试显示依赖于ComboBox2的datagridview1时,它只显示依赖于ComboBox1。但我希望comboBox2必须依赖 在comboBox1和datagridview1上必须依赖于comboBox1和comboBox2。 我该怎么做才能解决这个问题?

0 个答案:

没有答案