2013-09-27 4 views
0

시작하기 link 라디오 단추 유형 (열과 셀)의 사용자 지정 DataGridViewColumn을 만들었습니다.C# : DataGridView 사용자 지정 셀이 사용자 지정 속성을 잃습니다.

public class DataGridViewRadioButtonColumnEx : DataGridViewColumn 
{ 
    public DataGridViewRadioButtonColumnEx() 
    { 
     this.CellTemplate = new DataGridViewRadioButtonCell(); 
     this.ReadOnly = true; 
    } 
} 

public delegate void RadioButtonClickedHandler(bool state); 
public class DataGridViewRadioButtonCellEventArgs : EventArgs 
{ 
    bool _bChecked; 
    public DataGridViewRadioButtonCellEventArgs(bool bChecked) 
    { 
     _bChecked = bChecked; 
    } 

    public bool Checked 
    { 
     get { return _bChecked; } 
    } 
} 

public class DataGridViewRadioButtonCell : DataGridViewTextBoxCell 
{ 
    Point radioButtonLocation; 
    Size radioButtonSize; 
    bool _checked = false; 
    public bool Checked 
    { 
     get { return _checked; } 
     set { _checked = value; } 
    } 

    string _groupName = ""; 
    public string GroupName 
    { 
     get 
     { 
      return _groupName; 
     } 
     set 
     { 
      _groupName = value; 
     } 
    } 

    Point _cellLocation = new Point(); 
    System.Windows.Forms.VisualStyles.RadioButtonState _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal; 
    public event RadioButtonClickedHandler OnRadioButtonClicked; 

    public DataGridViewRadioButtonCell() 
    { 
    } 

    protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) 
    { 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 

     Point p = new Point(); 
     Size s = RadioButtonRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal); 

     p.X = cellBounds.Location.X + (cellBounds.Width/2) - (s.Width/2); 
     p.Y = cellBounds.Location.Y + (cellBounds.Height/2) - (s.Height/2); 

     _cellLocation = cellBounds.Location; 
     radioButtonLocation = p; 
     radioButtonSize = s; 

     if (_checked) 
      _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.CheckedNormal; 
     else 
      _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal; 

     RadioButtonRenderer.DrawRadioButton(graphics, radioButtonLocation, _cbState); 
    } 

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) 
    { 
     Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y); 

     if (p.X >= radioButtonLocation.X && p.X <= radioButtonLocation.X + radioButtonSize.Width && p.Y >= radioButtonLocation.Y && p.Y <= radioButtonLocation.Y + radioButtonSize.Height) 
     { 
      _checked = !_checked; 

      if (OnRadioButtonClicked != null) 
      { 
       OnRadioButtonClicked(_checked); 
       this.DataGridView.InvalidateCell(this); 
      } 
     } 
     base.OnMouseClick(e); 
    } 

    protected override void OnKeyUp(KeyEventArgs e, int rowIndex) 
    { 
     base.OnKeyUp(e, rowIndex); 
     if (e.KeyCode == Keys.Space) 
     { 
      _checked = true; 

      if (OnRadioButtonClicked != null) 
      { 
       OnRadioButtonClicked(_checked); 
       this.DataGridView.InvalidateCell(this); 
      } 
     } 
    } 

    /// <summary> 
    /// To Set the status of the Check-Box Header column 
    /// </summary> 
    /// <param name="flagIndicator">True - to mark checked state. False : To mark it as Unchecked.</param> 
    public void SetRadioButton(bool flagIndicator) 
    { 
     _checked = flagIndicator; 

     this.DataGridView.InvalidateCell(this); 
    } 
} 

그것은 잘 작동, 나는 (라디오 버튼이 속한 것을 이제 그룹에 사용)하지만 런타임에, 어딘가에, 클리어 "그룹 이름"라는 사용자 지정 속성을 추가했다.

public class CustomDataGridView : DataGridView 
{ 
    protected override void OnCellClick(DataGridViewCellEventArgs e) 
    { 
     if ((e.RowIndex > -1) && (e.ColumnIndex > -1)) 
     { 
      if ((this[e.ColumnIndex, e.RowIndex]).OwningColumn is DataGridViewRadioButtonColumnEx) 
      { 
       DataGridViewRadioButtonCell cell = this[e.ColumnIndex, e.RowIndex] as DataGridViewRadioButtonCell; 
       cell.Checked = !cell.Checked; 
       if (((DataGridViewRadioButtonCell)cell).Checked) 
       { 
        for (int i = 0; i < this.RowCount; i++) 
        { 
         if (cell.GroupName == (this[e.ColumnIndex, i] as DataGridViewRadioButtonCell).GroupName) 
          (this[e.ColumnIndex, i] as DataGridViewRadioButtonCell).Checked = false; 
        } 
       } 
      } 
     } 
     base.OnCellClick(e); 
     this.InvalidateColumn(e.ColumnIndex); 
    } 
} 

어떻게 그룹 이름 속성을 지울 수 있습니까?

감사합니다. 검도

LE : 발견! Visual Studio에서

public override object Clone() 
    { 
     var clone = (DataGridViewRadioButtonCell)base.Clone(); 

     clone.Checked = _checked; 
     clone.GroupName = _groupName; 
     clone.CellType = _cellType; 

     return clone; 
    } 

답변

0

: 나는 clone 메소드를 오버라이드 (override) 할 필요가 귀하의 재산의 선두로부터 마우스 오른쪽 버튼으로 클릭 - 모든 참조 찾기>. 또는 Setter에 GroupName의 중단 점을 넣고 디버깅하는 동안 변경되는 호출을 관찰 할 수 있습니다.

+0

감사합니다. 나는 이미이 모든 작업을 수행했습니다. – Kendo

+0

Setter가 액세스되지 않았기 때문에 예를 들어 설명하는 것이 어렵습니다. 처음부터 설정 되셨습니까? – nuke

+0

Setter는 초기화 될 때만 액세스됩니다. 'row.Cells.Add (새 DataGridViewRadioButtonCell() {GroupName = "g1"});' – Kendo

관련 문제