2013-01-11 2 views
2

DataGridView 셀에 사용자 지정 컨트롤이 있습니다. 체크 박스 항목 (CheckBoxComboBox)이 포함 된 콤보 상자입니다. 여기에 문제가 있습니다 : 1. CheckBoxComboBox 중 하나를 입력하고 일부 체크 상자 항목을 선택하십시오. CheckboxComboBox의 텍스트는 체크 된 항목의 csv 문자열입니다. 2. emtpy (확인 항목 없음) 인 다른 CheckboxComboBox 셀을 클릭하십시오.DataGridView 셀의 사용자 지정 컨트롤

결과 : 새 셀의 텍스트에 이전 셀의 텍스트가 포함됩니다. CheckBoxComboBox 셀을 클릭 한 다음 CheckBoxComboBox 셀이 아닌 CheckBoxComboBox 셀을 클릭하면 올바르게 작동합니다.

본인은이 문서를 기반으로 DataGridViewCell 사용자 정의를 구현 한 : How to: Host Controls in Windows Forms DataGridView Cells

나는 내 사용자 지정 DataGridViewEditingControl을 통해 디버깅, EditingControl.Tag이 업데이트되지 않은 것 같습니다.

그래서 나는 EditingControl이 다시 사용되는 문제가 있다고 가정합니다.

상황은 내가 시도 :

1. 재정의 DataGridViewCell.Clone()

public override object Clone() 
    { 
     DataGridViewCheckBoxComboBoxCell checkBoxComboBoxCell = base.Clone() as DataGridViewCheckBoxComboBoxCell; 
     if (checkBoxComboBoxCell != null) 
     { 
      checkBoxComboBoxCell.Tag = this.Tag; 
      checkBoxComboBoxCell.Values = this.Values; 


     } 
     return checkBoxComboBoxCell; 
    } 

2. 재정 DataGridViewCell.DetachEditingControl()

public override void DetachEditingControl() 
    { 
     DataGridView dataGridView = this.DataGridView; 

     if (dataGridView == null || dataGridView.EditingControl == null) 
     { 
      throw new InvalidOperationException("Cell is detached or its grid has no editing control."); 
     } 

     DataGridViewCheckBoxComboBoxCellEditingControl ctl = DataGridView.EditingControl as DataGridViewCheckBoxComboBoxCellEditingControl; 
     if (ctl != null) 
     { 
      //Just trying different things 
      ctl.EditingControlFormattedValue = String.Empty; 
      ctl.Text = string.Empty; 
      ctl.Tag = null; 
      ctl.Items.Clear(); 
     } 

     base.DetachEditingControl(); 
    } 

어떤 생각 방법 이 문제를 해결하려면? 감사.

편집 여기에 1

는 DataGridViewCheckBoxComboBoxColumn 클래스

class DataGridViewCheckBoxComboBoxColumn : DataGridViewColumn 
{ 
    public override object Clone() 
    { 
     DataGridViewCheckBoxComboBoxColumn that = (DataGridViewCheckBoxComboBoxColumn)base.Clone(); 

     return that; 
    } 

    private DataGridViewCheckBoxComboBoxCell _cell = null; 
    public DataGridViewCheckBoxComboBoxColumn() 
    { 
     _cell = new DataGridViewCheckBoxComboBoxCell(); 
     base.CellTemplate = _cell; 
    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return base.CellTemplate; 
     } 
     set 
     { 
      // Ensure that the cell used for the template is a DateCell. 
      if (value != null && 
       !value.GetType().IsAssignableFrom(typeof(DataGridViewCheckBoxComboBoxCell))) 
      { 
       throw new InvalidCastException("Must be a DataGridViewCheckBoxComboBoxColumn"); 
      } 
      base.CellTemplate = value; 
     } 
    } 

    public string Values 
    { 
     set 
     { 
      _cell.Tag = value; 
      this.Tag = value; 
     } 
     get 
     { 
      return _cell.Tag.ToString(); 
     } 
    } 
} 

편집 2 내 DataGridViewCheckBoxComboBoxCell 페인트를 무시()입니다. 이 메서드에 중단 점을 넣으면 셀을 클릭 할 때 두 번 호출됩니다. 처음으로 호출 할 때 formattedValue는 비어 있습니다. 그러나 두 번째로 formattedValue에는 이전 checkboxcombobox 셀의 잘못된 문자열이 포함됩니다.

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
            DataGridViewElementStates cellState, object value, object formattedValue, 
            string errorText, DataGridViewCellStyle cellStyle, 
            DataGridViewAdvancedBorderStyle advancedBorderStyle, 
            DataGridViewPaintParts paintParts) 
    {} 

두 번 호출되는 이유와 두 번째 호출에 올바른 셀의 형식이 잘못된 값이 포함 된 이유는 무엇입니까?

+0

DataGridView에 DataGridViewCheckBoxComboBoxCell을 추가하면 어떻게됩니까? –

답변

1

알아 냈어. 문제는 DetachEditingControl()에서 CheckBoxItem을 지울 필요가 있다는 것입니다.

public override void DetachEditingControl() 
    { 
     DataGridView dataGridView = this.DataGridView; 

     if (dataGridView == null || dataGridView.EditingControl == null) 
     { 
      throw new InvalidOperationException("Cell is detached or its grid has no editing control."); 
     } 

     DataGridViewCheckBoxComboBoxCellEditingControl ctl = DataGridView.EditingControl as DataGridViewCheckBoxComboBoxCellEditingControl; 
     if (ctl != null) 
     { 
      ctl.CheckBoxItems.Clear(); //Forgot to do this. 
      ctl.EditingControlFormattedValue = String.Empty; 
     } 

     base.DetachEditingControl(); 
    } 
+0

안녕하세요 웹 비슷한 문제가 있습니다. 나는 사용자가 내 comobox에 항목을 추가하고 항목 옆에있는 이미지를 클릭하여 항목을 제거 할 수있는 사용자 정의 comboBox를 만들었습니다. 다른 cellTypes (dataGridViewTextBoxCell, dataGridViewButtonCell 등) 및 사용자 정의 comboBox가있는 열이 있습니다. 만약 내가 하나 이상의 사용자 정의 콤보 상자가 그들 모두 자신의 항목 대신 동일한 editingControl 데이터를 사용하는 것 같아요 그리고 내 모든 comboBoxes 항목/데이터를 잃을 컨트롤의 다른 유형을 선택하십시오. 당신의 구현 방법에 대한 예를 살펴볼 수 있습니까? –

관련 문제