2012-02-06 3 views
1

구성 정보가 들어있는 DataGridView를 만들려고합니다.각 셀의 데이터 소스가 다른 DataGridView 콤보 상자

사용 가능한 값은 다른 열의 값을 기반으로하는 열 내의 각 행에 대해 변경 될 수 있으므로 comboBox 열에 단일 데이터 소스를 연결할 수 없습니다. 예 : 자동차를 선택하면 사용 가능한 색상이 해당 모델에 사용 가능한 색상으로 제한되어야합니다.

Car     ColorsAvailable 
Camry    {white,black} 
CRV     {white,black} 
Pilot    {silver,sage} 

dataGridView를 고려하는 이유는 연산자가 추가 자동차 행을 추가 할 수 있기 때문입니다.

이 유형의 UI를 구현하는 좋은 디자인은 무엇입니까?

답변

8

DataGridViewComboBoxCell에 별도로 DataSource을 설정할 수 있습니다

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == 0) // presuming "car" in first column 
    { // presuming "ColorsAvailable" in second column 
     var cbCell = dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell; 
     string[] colors = { "white", "black" }; 
     switch (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()) 
     { 
      case "Pilot": colors = new string[] { "silver", "sage" }; break; 
       // case "other": add other colors 
     } 

     cbCell.DataSource = colors; 
    } 
} 

당신의 색상 (그리고 어쩌면 자동차) 물론 열거 같은 강력한 유형 대신 내가 전환하고있어 문자열의 그 유형을 사용해야하는 경우 여기에 삽입하면 ...

+0

답변을 주셔서 감사합니다. 정확히 무엇이 필요한가요? – DarwinIcesurfer

관련 문제