001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Text;
008.
using
System.Windows.Forms;
009.
010.
namespace
Test_Grid_CheckBox
011.
{
012.
public
partial
class
Form1 : Form
013.
{
014.
CheckBox chkAll =
new
CheckBox();
015.
private
int
headerCheckboxRightMargin;
016.
017.
public
Form1()
018.
{
019.
InitializeComponent();
020.
}
021.
022.
private
void
Form1_Load(
object
sender, EventArgs e)
023.
{
024.
025.
}
026.
027.
public
void
AddCheckAll()
028.
{
029.
Rectangle rect;
030.
try
031.
{
032.
rect =
new
Rectangle();
033.
chkAll.Size =
new
Size(14, 14);
034.
rect.X = dataGridView1.RowHeadersWidth + (dataGridView1.Columns[0].Width/2)-6;
035.
rect.Y = 5;
036.
chkAll.Location = rect.Location;
037.
chkAll.CheckedChanged +=
new
System.EventHandler(
new
EventHandler(ckBox_CheckedChanged));
038.
dataGridView1.Controls.Add(chkAll);
039.
}
040.
catch
(Exception ex)
041.
{
042.
}
043.
}
044.
045.
private
void
button1_Click(
object
sender, EventArgs e)
046.
{
047.
int
i;
048.
DataGridViewTextBoxColumn col1 =
new
DataGridViewTextBoxColumn();
049.
DataGridViewTextBoxColumn col2 =
new
DataGridViewTextBoxColumn();
050.
DataGridViewCheckBoxColumn chk1 =
new
DataGridViewCheckBoxColumn();
051.
052.
chk1.Name =
"chk1"
;
053.
col1.Name =
"col1"
;
054.
col2.Name =
"col2"
;
055.
chk1.HeaderText =
""
;
056.
col1.HeaderText =
"col1"
;
057.
col2.HeaderText =
"col2"
;
058.
059.
dataGridView1.Columns.Add(chk1);
060.
dataGridView1.Columns.Add(col1);
061.
dataGridView1.Columns.Add(col2);
062.
dataGridView1.AllowUserToAddRows =
false
;
063.
AddCheckAll();
064.
065.
066.
067.
for
(i = 0; i <= 100; i++)
068.
069.
{
070.
dataGridView1.Rows.Add(
new
object
[] {0, i,
"Test"
});
071.
}
072.
073.
var checkboxHeaderCellRect = dataGridView1.GetCellDisplayRectangle(0, -1,
false
);
074.
headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - chkAll.Width) / 2;
075.
076.
}
077.
078.
private
void
ckBox_CheckedChanged(
object
sender, EventArgs e)
079.
{
080.
081.
foreach
(DataGridViewRow dr
in
dataGridView1.Rows)
082.
{
083.
switch
(chkAll.Checked)
084.
{
085.
case
true
:
086.
dr.Cells[0].Value= 1;
087.
break
;
088.
case
false
:
089.
dr.Cells[0].Value = 0;
090.
break
;
091.
default
:
092.
dr.Cells[0].Value = 0;
093.
break
;
094.
}
095.
}
096.
dataGridView1.CurrentCell =
null
;
097.
}
098.
099.
private
void
dataGridView1_CellPainting(
object
sender, DataGridViewCellPaintingEventArgs e)
100.
{
101.
if
(e.RowIndex == -1 && e.ColumnIndex == 0)
102.
ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
103.
}
104.
105.
private
void
ResetHeaderCheckBoxLocation(
int
ColumnIndex,
int
RowIndex)
106.
{
107.
108.
Rectangle oRectangle =
this
.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex,
false
);
109.
110.
Point oPoint =
new
Point();
111.
112.
oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - chkAll.Width);
113.
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - chkAll.Height) / 2 + 1;
114.
115.
if
(oPoint.X < oRectangle.X)
116.
{
117.
chkAll.Visible =
false
;
118.
}
119.
else
120.
{
121.
chkAll.Visible =
true
;
122.
}
123.
chkAll.Location = oPoint;
124.
125.
126.
}
127.
128.
129.
}
130.
}