2012-03-22 3 views
3

안녕하세요 프로그래머, 같은 셀에 확인란과 레이블을 추가해야합니다. 어떻게하는지 알고 있습니다. 확인란에 datagridviewcolumn을 만들 수 있지만 확인란을 표시하고 레이블을 표시 할 위치 만 표시합니다. 아무도이 악몽에서 나를 깨울 수 있습니까? 미리 감사드립니다. DataGridview의 동일한 셀에 확인란과 레이블을 추가하는 방법은 무엇입니까?

+0

당신은 템플릿 열을 사용할 수 있습니다. 둘 다 제어하십시오. –

+0

winforms에서 필요합니다. 사용할 수 있습니까 ?? –

답변

4

나는 상자 밖의 CheckBoxCell로는 가능하지 않다고 생각합니다.

다음은 확인란 열을 하위 클래스로 지정하고 레이블을 그리는 사용자 정의 페인팅을 수행하는 구현입니다. 레이블 텍스트에 대한 바인딩 제공 (현재 레이블 텍스트를 직접 설정해야 함)과 같이 다양한 방식으로 확장 할 수 있습니다. 내 코드는 this forum post에서 많이 빌려옵니다. 여기

using System; 
using System.Windows.Forms; 
using System.Drawing; 

public class MyDGVCheckBoxColumn : DataGridViewCheckBoxColumn 
{ 
    private string label; 

    public string Label 
    { 
     get 
     { 
      return label; 
     } 
     set 
     { 
      label = value; 
     } 
    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return new MyDGVCheckBoxCell(); 
     } 
    } 
} 

public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell 
{ 
    private string label; 

    public string Label 
    { 
     get 
     { 
      return label; 
     } 
     set 
     { 
      label = value; 
     } 

    } 

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

     // the base Paint implementation paints the check box 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 

     // Get the check box bounds: they are the content bounds 
     Rectangle contentBounds = this.GetContentBounds(rowIndex); 

     // Compute the location where we want to paint the string. 
     Point stringLocation = new Point(); 

     // Compute the Y. 
     // NOTE: the current logic does not take into account padding. 
     stringLocation.Y = cellBounds.Y + 2; 


     // Compute the X. 
     // Content bounds are computed relative to the cell bounds 
     // - not relative to the DataGridView control. 
     stringLocation.X = cellBounds.X + contentBounds.Right + 2; 


     // Paint the string. 
     if (this.Label == null) 
     { 
      MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn; 
      this.label = col.Label; 
     } 

     graphics.DrawString(
     this.Label, Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation); 

    } 

} 

코드는 사용을 보여주고있다 :

private void Form1_Load(object sender, EventArgs e) 
{ 
    MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn(); 
    col.Label = "hippo"; 
    col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; 
    this.dataGridView1.Columns.Add(col); 
    this.dataGridView1.RowCount = 5; 

    foreach (DataGridViewRow row in this.dataGridView1.Rows) 
    { 
     if (row.Index % 2 == 0) 
     { 
      ((MyDGVCheckBoxCell)row.Cells[0]).Label = "kitten"; 
     } 
    } 
} 
+0

도움을 많이 주셔서 감사합니다. 시간을 절약 할 수 있습니다. –

+0

내가 물어보고 싶은 한 가지는 DataGridViewCheckBox의 checkState를 변경하는 중 이벤트를 만드는 방법입니다. –

+1

@PrakashKunwar이 경우 celldirtystatechanged 이벤트를 사용할 수 있습니다 (다른 방법이 있지만 가장 자주 찾는이 방법이 있습니다).이 질문을보십시오. http://stackoverflow.com/questions/6468263/datagridviewcheckboxcolumn-how-to-update -bound-datasource-on-property-i-6469559 # 6469559 –

관련 문제