2010-06-24 7 views
2

OMG 2 일 동안이 작업을 수행했지만 여전히 솔루션을 얻을 수 없습니다. 이것은 나의 시나리오입니다 :다른 콤보 상자에서 선택한 색인에 따라 콤보 상자 채우기

나는 3 개의 콤보 박스 컨트롤을 가지고 있으며 데이터 셋의 데이터를 다이나믹하게로드해야합니다.

  • cbCode1은로드 양식으로 채워집니다. cbCode1에서 선택한 인덱스 cbCode1에서 선택한 인덱스 데이터가 datset "설명"과 "id_code"두 필드에서 오는

변경시 cbCode3는

  • 가 채워 바뀌면
  • cbCode2 채워이다. 따라서 설명의 값을 ID와 연관시키고 싶습니다. I 부하 형태 ..이 나중에

    요청해야하기 때문에이있다 :

    cbCode1.DataSource D.Tables = [0]; cbCode1.DisplayMember = D.Tables [0] .Columns [ "description"]. ColumnName; cbCode1.ValueMember = D.Tables [0] .Columns [ "id_code"]. ColumnName;

    이제 때 cbcode1 변경에서 선택한 인덱스 :

    private void cbCode1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
    
         // Get dataset from DB 
         DataSet D = new DataSet(); 
         D = reason_get(1, 1, 1); // IdStation, Active, Level 
    
         // Ensure the cbcode2 is cleared 
         cbCode2.Items.Clear(); 
         string SelectedValue = cbCode1.SelectedValue.ToString(); 
    
         foreach (DataRow row in D.Tables[0].Rows) 
         { 
    
    
          if (row["id_parent_code"].ToString() == SelectedValue) 
          { 
    
           cbCode2.DataSource = D.Tables[0]; 
           cbCode2.DisplayMember = D.Tables[0].Columns["description"].ColumnName; 
           cbCode2.ValueMember = D.Tables[0].Columns["id_code"].ColumnName; 
    
           cbCode2.SelectedIndex = 0; 
    
          } 
         } 
    
    
    
        } 
    

    이 코드는 내가 내가 잘못 모르는 작동하지 않습니다. 이 문제를 해결하는 데 도움을 주시겠습니까? 사전에

    감사합니다!

    초보자 프로그래머

    ..

  • +0

    'cbCode1' 및'cbCode2'보다 더 나은 변수 이름을 사용하십시오. 값이 의미하는 것을 실제로 말하는 이름을 사용하십시오. – unholysampler

    답변

    0

    나는 전에이 작업을 수행하지 않은하지만 난 DataGridView에 대한 비슷한 일을했다. 당신이해야 할 일은 액션 리스너를 사용하는 것입니다. 기본적으로 첫 번째 콤보 상자를 채운 다음 actionListener를 추가하여 선택한 인덱스가 변경 될 때 호출됩니다. 해당 액션 리스너 내에서 두 번째 콤보 상자를 채 웁니다.

    -1

    시도 코드 아래처럼, 그것을 할 수 ...

    개인 무효를 Form1_Load (개체 보낸 사람, EventArgs입니다 전자) { FillCountry();

    } 
    
        private void FillCountry() 
        { 
         string str = "SELECT CountryID, CountryName FROM Country"; 
         SqlCommand cmd = new SqlCommand(str,con); 
         //cmd.Connection = con; 
         //cmd.CommandType = CommandType.Text; 
         // cmd.CommandText = "SELECT CountryID, CountryName FROM Country"; 
         DataSet objDs = new DataSet(); 
         SqlDataAdapter dAdapter = new SqlDataAdapter(); 
         dAdapter.SelectCommand = cmd; 
         con.Open(); 
         dAdapter.Fill(objDs); 
         con.Close(); 
         comboBox1.ValueMember = "CountryID"; 
         comboBox1.DisplayMember = "CountryName"; 
         comboBox1.DataSource = objDs.Tables[0]; 
        } 
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
         if (comboBox1.SelectedValue.ToString() != "") 
         { 
          int CountryID = Convert.ToInt32(comboBox1.SelectedValue.ToString()); 
          FillStates(CountryID); 
          comboBox3.SelectedIndex = 0; 
         } 
        } 
    
        private void FillStates(int countryID) 
        { 
         string str = "SELECT StateID, StateName FROM State WHERE CountryID [email protected]"; 
         SqlCommand cmd = new SqlCommand(str, con); 
         // SqlConnection con = new SqlConnection(Con); 
         // cmd.Connection = con;   
         // string str="SELECT StateID, StateName FROM State WHERE CountryID [email protected]"; 
         // cmd.Connection = con; 
         //cmd.CommandType = CommandType.Text; 
         // cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID [email protected]"; 
         cmd.Parameters.AddWithValue("@CountryID", countryID); 
         DataSet objDs = new DataSet(); 
         SqlDataAdapter dAdapter = new SqlDataAdapter(); 
         dAdapter.SelectCommand = cmd; 
         con.Open(); 
         dAdapter.Fill(objDs); 
         con.Close(); 
         if (objDs.Tables[0].Rows.Count > 0) 
         { 
          comboBox2.ValueMember = "StateID"; 
          comboBox2.DisplayMember = "StateName"; 
          comboBox2.DataSource = objDs.Tables[0]; 
         } 
        } 
    
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
        { 
         int StateID = Convert.ToInt32(comboBox2.SelectedValue.ToString()); 
         FillCities(StateID); 
        } 
    
    
        private void FillCities(int stateID) 
        { 
         //SqlConnection con = new SqlConnection(str,con); 
         string str = "SELECT CityID, CityName FROM City WHERE StateID [email protected]"; 
         SqlCommand cmd = new SqlCommand(str,con); 
         // cmd.Connection = con; 
         //cmd.CommandType = CommandType.Text; 
         // cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateID [email protected]"; 
         cmd.Parameters.AddWithValue("@StateID", stateID); 
         DataSet objDs = new DataSet(); 
         SqlDataAdapter dAdapter = new SqlDataAdapter(); 
         dAdapter.SelectCommand = cmd; 
         con.Open(); 
         dAdapter.Fill(objDs); 
         con.Close(); 
         if (objDs.Tables[0].Rows.Count > 0) 
         { 
          comboBox3.DataSource = objDs.Tables[0]; 
          comboBox3.DisplayMember = "CityName"; 
          comboBox3.ValueMember = "CItyID"; 
    
         } 
    
    관련 문제