2013-05-16 3 views
1

저는 여기에서 좀 새로운데, 제가 만들고있는 프로그램에 대해 많은 질문을하지만, 저에게 가장 버그가있는 것이 제목에 있습니다.다른 콤보 상자를 기반으로 콤보 상자를 채우는 방법?

DataSet ds = new DataSet(); 
      MySqlDataAdapter da = new MySqlDataAdapter("SELECT cveestado, nomestado FROM tbEstado", conexion); 
      da.Fill(ds, "FillDropDown"); 
      cbEstado.DisplayMember = "Nomestado"; 
      cbEstado.ValueMember = "CveEstado"; 
      cbEstado.DataSource = ds.Tables["FillDropDown"]; 
      conexion.Close(); 

그리고 사용자가 선택하는 무엇을 기준으로, 내가 처음 선택에 따라 다른 콤보를 채우려 :

는 기본적으로, 나는이를 사용하여 채우기 콤보 상자를 보유하고 있습니다.

는 지금까지이있다, 그러나 그것은 작품을 does't : 사용자가 "타바스코"를 선택하면

private void cbEstado_TextChanged(object sender, EventArgs e) 
    { 
     if (cbEstado.SelectedValue.ToString() == "Tabasco") 
     { 
      try 
      { 
       DataSet ds = new DataSet(); 
       MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion); 
       da.Fill(ds, "FillDropDown"); 
       cbMunicipio.DisplayMember = "Nommunicipio"; 
       cbMunicipio.ValueMember = "Cvemunicipio"; 
       cbMunicipio.DataSource = ds.Tables["FillDropDown"]; 
       conexion.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
    } 

기본적으로, 나는 두 번째 콤보 상자는 타바스코의 지방 자치 단체와 채우려.

내 SQL 코드는 경우, 다음과 같다 :

create table tbEstado 
(cveEstado int not null, 
nomEstado varchar(45) not null, 
constraint pkcveEstado primary key (cveEstado) 
)engine=innodb; 

create table tbMunicipio 
(cveMunicipio int not null, 
nomMunicipio varchar(45) not null, 
cveEstado int not null, 
constraint pkcveMunicipio primary key (cveMunicipio), 
constraint fkcveEstado foreign key (cveEstado) references tbEstado(cveEstado) 
)engine=innodb; 

감사합니다!

편집

대답, 감사 https://stackoverflow.com/users/1197518/steve에있다 :

private void cbEstado_TextChanged(object sender, EventArgs e) 
     { 
      if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27) 
      { 
       try 
       { 
        DataSet ds = new DataSet(); 
        MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion); 
        da.Fill(ds, "FillDropDown"); 
        cbMunicipio.DisplayMember = "Nommunicipio"; 
        cbMunicipio.ValueMember = "Cvemunicipio"; 
        cbMunicipio.DataSource = ds.Tables["FillDropDown"]; 
        conexion.Close(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 
     } 
+0

그리고 무엇이 문제입니까? – Steve

+0

안녕하세요 @ 스티브! 나는 첫 번째 콤보 박스 (cbEstado)에서 내가 선택한 것에 기반하여 두 번째 콤보 박스 (cbMunicipio)를 채우고 싶다. –

+1

WPF 클라이언트 측에서'System.Data' 사용을 권장하지 않습니다. 적절한 데이터 모델을 작성하십시오. 그리고 DB 접근 로직을 코드 뒤에서 옮기십시오. WPF의 코드에서 UI 요소를 조작하지 마십시오. 적절한 ViewModel을 만들고 DataBinding을 사용하십시오. –

답변

0

당신은 열 CveEstado에 연결되어있는 SelectedValue 속성을 확인하고 있습니다.

이 열이 아마 실제 문제를 해결하지만, 귀하의 질문에도 코멘트에 제공 한 조언을 확인하시기 바랍니다 것입니다 Tabasco

if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27) 
    { 
     try 
     { 
     ....... 

하지 문자열 숫자가 포함

+0

감사합니다. @ 스티브! 이것은 완벽하게 작동합니다 : D –

0
당신은 DataView를 사용할 수 있습니다

당신이 할 수있는 cbSample1_SelectedIndexChange 이벤트에

cbEstado.Datasource =ds.Tables["State"]; 
cbMunicipio.Datasource = new Dataview(ds.Tables["Muncipalities"];); 

: 예를 들어

(cbMunicipio.DataSource as DataView).RowFilter = "cveEstado='" + cbEstado.SelectedValue.ToString() + "'"; 
관련 문제