반응형
class DGVColumnHeader : DataGridViewColumnHeaderCell
{
private Rectangle CheckBoxRegion;
private bool checkAll = false;
protected override void Paint(Graphics graphics,
Rectangle clipBounds, 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);
graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
CheckBoxRegion = new Rectangle(
cellBounds.Location.X + 1,
cellBounds.Location.Y + 2,
25, cellBounds.Size.Height - 4);
if (this.checkAll)
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);
Rectangle normalRegion =
new Rectangle(
cellBounds.Location.X + 1 + 25,
cellBounds.Location.Y,
cellBounds.Size.Width - 26,
cellBounds.Size.Height);
graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//Convert the CheckBoxRegion
Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
this.checkAll = !this.checkAll;
if (rec.Contains(e.Location))
{
this.DataGridView.Invalidate();
}
base.OnMouseClick(e);
}
public bool CheckAll
{
get { return this.checkAll; }
set { this.checkAll = value; }
}
}
//Create the object of DGVColumnHeader
DGVColumnHeader dgvColumnHeader;
private void Form1_Load(object sender, EventArgs e)
{
//initialize DGVColumnHeader object
dgvColumnHeader = new DGVColumnHeader();
//Add columns dynamically to gridview
dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
dataGridView1.Columns[0].HeaderCell = dgvColumnHeader;
//Data Binding
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Rows.Add("100", "101");
dt.Rows.Add("102", "103");
dataGridView1.DataSource = dt;
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = dgvColumnHeader.CheckAll;
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
//Escalate Editmode
this.dataGridView1.EndEdit();
string re_value = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();
this.dataGridView1.Rows[i].Cells[0].Value = "true";
}
}
}
출처 : http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx
{
private Rectangle CheckBoxRegion;
private bool checkAll = false;
protected override void Paint(Graphics graphics,
Rectangle clipBounds, 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);
graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
CheckBoxRegion = new Rectangle(
cellBounds.Location.X + 1,
cellBounds.Location.Y + 2,
25, cellBounds.Size.Height - 4);
if (this.checkAll)
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);
Rectangle normalRegion =
new Rectangle(
cellBounds.Location.X + 1 + 25,
cellBounds.Location.Y,
cellBounds.Size.Width - 26,
cellBounds.Size.Height);
graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//Convert the CheckBoxRegion
Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
this.checkAll = !this.checkAll;
if (rec.Contains(e.Location))
{
this.DataGridView.Invalidate();
}
base.OnMouseClick(e);
}
public bool CheckAll
{
get { return this.checkAll; }
set { this.checkAll = value; }
}
}
//Create the object of DGVColumnHeader
DGVColumnHeader dgvColumnHeader;
private void Form1_Load(object sender, EventArgs e)
{
//initialize DGVColumnHeader object
dgvColumnHeader = new DGVColumnHeader();
//Add columns dynamically to gridview
dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
dataGridView1.Columns[0].HeaderCell = dgvColumnHeader;
//Data Binding
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Rows.Add("100", "101");
dt.Rows.Add("102", "103");
dataGridView1.DataSource = dt;
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = dgvColumnHeader.CheckAll;
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
//Escalate Editmode
this.dataGridView1.EndEdit();
string re_value = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();
this.dataGridView1.Rows[i].Cells[0].Value = "true";
}
}
}
출처 : http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx
반응형