[C#] DataGridView RowSelector Numbering
2014. 8. 6. 17:38ㆍCoders
수많은 상용 그리드들은, RowSelector 에 Numbering 기능을 제공합니다. 하지만, 일반적으로? 또는 테스트 용도로 쓰이는 윈도우 기본 제공 컨트롤인 DataGridView 는 RowSelector 에 숫자를 넣을 수 있는 프로퍼티 등이 없습니다.
예제 코딩과 같은 방법으로 RowSelector에 Numbering 을 할 수 있습니다.
※ 이를 응용하면, 뭐든 쓰거나 그릴 수 있겠죠.
※ 첨부된 이미지는 그냥 SSMS 결과 테이블에 넘버링이 되니까 붙여 넣은 겁니다. 우측 정렬이 되어 있네요.
예제 입니다.
public TestForm() { InitializeComponent(); this.myDataGridView.RowPostPaint -= myDataGridView_RowPostPaint; this.myDataGridView.RowPostPaint += myDataGridView_RowPostPaint; } private void myDataGridView_RowPostPaint( object sender, DataGridViewRowPostPaintEventArgs e) { var grid = sender as DataGridView; //찍을 로우 인덱스는 Zero-base 가 아닌 1부터 var rowIdx = (e.RowIndex + 1).ToString(); //폰트는 아무거나 지정 가능 using (StringFormat rightFormat = new StringFormat()) using (Font font = new System.Drawing.Font(this.Font, FontStyle.Regular)) { //숫자 매길 거니까, 우측 정렬. rightFormat.Alignment = StringAlignment.Far; rightFormat.LineAlignment = StringAlignment.Center; //로우셀렉터에서 우측 정렬로 숫자를 찍으니, //바운드는 좀 줄여야 함. var headerBounds = new Rectangle( e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth - 10, e.RowBounds.Height); //숫자를 쓴다. e.Graphics.DrawString(rowIdx, font, SystemBrushes.ControlText, headerBounds, rightFormat); } }
'Coders' 카테고리의 다른 글
[MSSQL] 현재 블로킹중인 spid, 쿼리 얻기 (0) | 2014.08.28 |
---|---|
Oracle 클라이언트 라이브러리를 로드하려는 시도에서 BadImageFormatException을 Throw했습니다. 32비트 Oracle 클라이언트 구성 요소가 설치된 상태에서 64비트 모드로 실행할 경우 이러한 문제가 발생.. (0) | 2014.07.30 |
Text File Line Counter (1) | 2014.06.27 |