2015-01-22 5 views
1

두 개의 콤보 상자에 두 개의 서로 다른 정보가 있어야합니다.두 콤보 상자 항목을 함께 맞추는 방법

1.cb1 : information_schema.tables (이 여러 테이블 표시)에서 table_name을 선택하십시오.
2.cb2 : 열 이름으로 채워야합니다.

예 : 나는 같은 속성 CB1에 세 개의 테이블을했지만 EmpName (tblLondon, tblBerlin, tblRom, ...)

가 지금은 두 번째 comboboxe 열 EmpName에 표시를하고 싶어 열에서 다른 값을 가질 내가 처음 콤보 상자에서 테이블을 선택할 때마다 동적으로.

cb1[tblLondon]       cb2[John,Mavis,Chris,Mike..] 

또는

cb1[tblBerlin]       cb2[Günther,Peter, Sophie,Sunny, ..] 

이 유 PLZ이 코드는 (콤보)

그리고 지금 작업하고 내 CB1을 채우고 밖으로

string C = ConfigurationManager.ConnectionStrings[""].ConnectionString; 
     SqlConnection con = new SqlConnection(C); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC"); 
     try 
     { 
      // Open connection, Save the results in the DT and execute the spProc & fill it in the DT 
      con.Open(); 
      SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
      dt = new DataTable(); 
      adapter.Fill(dt); 
      cbTbl.DisplayMember = "TABLE_NAME"; 
      cbTbl.ValueMember = "TABLE_NAME"; 
      //Fill combobox with data in DT 
      cbTbl.DataSource = dt; 
      // Empty bzw. clear the combobox 
      cbTbl.SelectedIndex = -1; 

좀 도와 줄래 난 정말하지 않습니다 어떻게 가야할지 알고있다.

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
     { 


     } 
+0

을해야한다 "SelectedValueChanged" [ComboBox.SelectedIndexChanged] (https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx) 이벤트를 조사하고 싶습니다. – Anthony

+0

디자인 타임에 모든 테이블을 모르기 때문에 information_schema.tables를 사용하고 있다고 가정합니까? – Crowcoder

+0

언제든지 테이블 이름을 동적으로 변경할 수 있기를 원하기 때문에 사용하고 있습니다 – mikybrain

답변

0

이 데이터가 SQL 서버 또는 다른 데이터베이스에있는 경우 SelectedIndexChange 이벤트를 사용하고 해당 ID에 대한 항목을로드해야합니다 (일치 ID에 Display Member 및 Value Member 사용).

는 .IT가 당신에게 도움이 될 this을 살펴

편집 :
은 아래 코드와 함께이 작업을 수행 할 수 있습니다 (당신이 데이터베이스를 사용하지 않는 경우)
당신은 cb1.SelectedIndexChange에서이 코드를 사용한다 (또는 값)

 var cb2items = new Dictionary<int, string> {{1, "Name"}, {1, "anotherName"},{2,"Name"},{2, "anotherName"}}; // use the number for parent Id in cb1 
     foreach (var item in cb2items) 
     { 
      if (item.Key == int.Parse(comboBox1.SelectedValue.ToString())) 
      { 
       comboBox2.Items.Add(item); 
      } 
     } 

편집 2 :

사용 cb1.SelectedValueChange이 코드 :

string C = ConfigurationManager.ConnectionStrings[""].ConnectionString; 
    SqlConnection con = new SqlConnection(C); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = con; 
    cmd.CommandText = ("SELECT ... WHERE TableName = cb1.SelectedValue"); 
     spProc & fill it in the DT 
     con.Open(); 
     SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
     dt = new DataTable(); 
     adapter.Fill(dt); 
     cbTbl.DisplayMember = "TABLE_NAME"; 
     cbTbl.ValueMember = "TABLE_NAME"; 
     cbTb2.DataSource = dt; 

그리고 테이블 이름의 항목을 입력으로 다시 보내는 프로 시저를 작성할 수도 있습니다. 프로 시저를 사용하면 코드 성능이 향상됩니다.

편집 3 :

cbTb2.SelectedValueChange 이벤트에서이 코드를 넣어 :

 try 
     { 
      int a = int.Parse(cbTB2.SelectedValue.ToString()); 
     } 
     catch { } 
+0

Thnx하지만 SQL Server를 사용하여 cb1을 채 웁니다. 도움? – mikybrain

+0

코드를 변경했지만 데이터베이스 필드를 모르기 때문에 쿼리를 완료 할 수 없습니다. –

+0

Thannx 대단히 나는 내 운을 시험해보고있다. – mikybrain

0

당신은 콤보 상자의 이벤트를 볼 수도 있습니다 "의 SelectedIndexChanged는"나에게 당신은 아마거야

+0

안녕하세요 저는 C# winforms에 익숙하지 않습니다. 여기서 shd는 선택된 인덱스를 변경하여 cb2가 채워지도록 적용합니다. u가 나에게 예제를 준다면 새로울 것입니다 – mikybrain

+0

가장 단순한 예제 – mysticcode

+0

Thnx로 업데이트 된 답변을 얻었지만 여전히 얻지 못했습니다. SQL Server를 사용하여 cb1을 채 웁니다. 테이블이 선택되어 있으면 정의 된 열 이름이 cb2로 채워 져야합니다. 그게 가능하니? – mikybrain

관련 문제