c# -- datagridview 일련번호 사용, CheckBox 사용, Column 크기 고정
참고: https://baidharsh.wordpress.com/2012/01/26/display-row-number-in-winforms-datagridview/
https://www.c-sharpcorner.com/UploadFile/deveshomar/adding-checkbox-column-in-datagridview-in-C-Sharp-window-forms/
https://stackoverflow.com/questions/11843488/how-to-detect-datagridview-checkbox-event-change#15011844
** Row header 에 자동 일련번호 넣기
// row header 에 자동 일련번호 넣기
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
StringFormat drawFormat = new StringFormat();
//drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
using (Brush brush = new SolidBrush(Color.Red))
{
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font,
brush, e.RowBounds.Location.X +35, e.RowBounds.Location.Y+4, drawFormat);
}
}
** 마우스로 row header width 조절 못하게 하기.
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
** 마우스로 column size 조절 못하게 하기
this.dataGridView1.Columns[0].Resizable = DataGridViewTriState.False;
** 컬럼 수정 못하게 하기.
this.dataGridView1.Columns[1].ReadOnly = true;
** DataGridView 내부 checkBox 사용시, check 상태 반영 안되는 경우를 막기위한 코드
// checkBox 변경 상태를 확실히 반영하기 위함.
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
< 실행 결과 >
< 소스코드>
--