안녕하세요 프로그래머, 같은 셀에 확인란과 레이블을 추가해야합니다. 어떻게하는지 알고 있습니다. 확인란에 datagridviewcolumn을 만들 수 있지만 확인란을 표시하고 레이블을 표시 할 위치 만 표시합니다. 아무도이 악몽에서 나를 깨울 수 있습니까? 미리 감사드립니다. DataGridview의 동일한 셀에 확인란과 레이블을 추가하는 방법은 무엇입니까?
답변
나는 상자 밖의 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";
}
}
}
도움을 많이 주셔서 감사합니다. 시간을 절약 할 수 있습니다. –
내가 물어보고 싶은 한 가지는 DataGridViewCheckBox의 checkState를 변경하는 중 이벤트를 만드는 방법입니다. –
@PrakashKunwar이 경우 celldirtystatechanged 이벤트를 사용할 수 있습니다 (다른 방법이 있지만 가장 자주 찾는이 방법이 있습니다).이 질문을보십시오. http://stackoverflow.com/questions/6468263/datagridviewcheckboxcolumn-how-to-update -bound-datasource-on-property-i-6469559 # 6469559 –
- 1. Datagridview의 데이터를 다른 클래스에 추가하는 방법은 무엇입니까?
- 2. 개인 데이터를 개별 셀에 추가하는 방법은 무엇입니까?
- 3. C++ 인터페이스를 구현하는 레이블을 추가하는 방법은 무엇입니까?
- 4. UITableView 셀에 이미지를 추가하는 방법은 무엇입니까?
- 5. 테이블 셀에 하위 텍스트를 추가하는 방법은 무엇입니까?
- 6. NSTableView 셀에서 패딩을 셀에 추가하는 방법은 무엇입니까?
- 7. JTable 셀에 JComboBox를 추가하는 방법은 무엇입니까?
- 8. JTable 셀에 이미지를 추가하는 방법은 무엇입니까?
- 9. JQGrid 테이블 셀에 데이터를 추가하는 방법은 무엇입니까?
- 10. iOS에서 테이블보기 셀에 두 개의 레이블을 차례로 넣는 방법은 무엇입니까?
- 11. DataGridView의 특정 셀에 대한 강조 색
- 12. datagridview의 특정 셀에 행 메모 추가
- 13. 셀에 체크 표시가있는 경우 레이블을 파란색으로 만듭니다.
- 14. tableview 셀에 텍스트 레이블을 표시하는 방법
- 15. Dojo 차트 작성 : 데이터 스토어의 레이블을 Axis에 추가하는 방법은 무엇입니까?
- 16. TFS API를 사용하여 파일에 레이블을 추가하는 가장 좋은 방법은 무엇입니까?
- 17. 막대 차트의 레이블을 회전하고 코어 플롯에서 오프셋을 추가하는 방법은 무엇입니까?
- 18. Interface Builder에서 UITableViewCell의 contentView에 사용자 정의 레이블을 추가하는 방법은 무엇입니까?
- 19. Highcharts Javascript 라이브러리의 산점도 점에 여분의 레이블을 추가하는 방법은 무엇입니까?
- 20. matlab figure의 맨 오른쪽에 x 축에 레이블을 추가하는 방법은 무엇입니까?
- 21. 텍스트 레이블을 기준으로 MBProgressHUD에 버튼을 추가하는 방법은 무엇입니까?
- 22. Java GUI에 다중 행 레이블을 추가하는 방법은 무엇입니까?
- 23. 동일한 기능에 매개 변수를 추가하는 방법은 무엇입니까?
- 24. 동일한 변수를 두 번 추가하는 방법은 무엇입니까?
- 25. iPhone SDK : 표 셀에 여러 라벨을 추가하는 방법은 무엇입니까?
- 26. iPhone에서 settings.bundle을 사용하여 맞춤형 셀에 이미지를 추가하는 방법은 무엇입니까?
- 27. C#에서 Intellisense를 DataGridView 셀에 추가하는 방법은 무엇입니까?
- 28. 테이블 셀에 클릭 가능한 버튼을 추가하는 방법은 무엇입니까?
- 29. vaadin에서 테이블의 셀에 클릭 리스너를 추가하는 방법은 무엇입니까?
- 30. 그룹화 된 테이블보기에서 셀에 UISwitch를 추가하는 방법은 무엇입니까?
당신은 템플릿 열을 사용할 수 있습니다. 둘 다 제어하십시오. –
winforms에서 필요합니다. 사용할 수 있습니까 ?? –