Sunday, May 11, 2014

Adding Checkbox (Select All) to the DataGridView and hiding the control onscrolling

Find the below code to add Checkbox to the grid and also hiding the control on scrolling.

private System.Windows.Forms.DataGridView dataGridView1;
private int headerCheckboxRightMargin;
DataGridViewCheckBoxColumn c1;
CheckBox ckBox;

private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(26, 36);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(174, 133);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1181, 493);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

private void Form1_Load(object sender, EventArgs e)
{

//Creating datatable object.
DataTable dtEmp = new DataTable();

//Creating datacolumns.
DataColumn colID = new DataColumn("ID");
DataColumn colName = new DataColumn("Name");
DataColumn colGender = new DataColumn("Gender");
DataColumn colManager = new DataColumn("Manager");

//Assign datatype for the columns.
colID.DataType = System.Type.GetType("System.String");
colName.DataType = System.Type.GetType("System.String");
colGender.DataType = System.Type.GetType("System.String");
colManager.DataType = System.Type.GetType("System.String");

//Add the columns to the table.
dtEmp.Columns.Add(colID);
dtEmp.Columns.Add(colName);
dtEmp.Columns.Add(colGender);
dtEmp.Columns.Add(colManager);

//Create a newrow in datatable.
DataRow row = dtEmp.NewRow();

//Assign values to the columns.
row[colID] = "1100";
row[colName] = "John";
row[colGender] = "Male";
row[colManager] = "Michel";

dtEmp.Rows.Add(row);

row = dtEmp.NewRow();
row[colID] = "1101";
row[colName] = "Miranda";
row[colGender] = "Female";
row[colManager] = "Michel";

dtEmp.Rows.Add(row);

c1 = new DataGridViewCheckBoxColumn();
c1.Name = "";
c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.Columns.Add(c1);

ckBox = new CheckBox();
//Get the column header cell bounds
ckBox.Size = new Size(18, 18);
this.dataGridView1.Controls.Add(ckBox);

var checkboxHeaderCellRect = dataGridView1.GetCellDisplayRectangle(0, -1, false);
headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - ckBox.Width) / 2;

dataGridView1.DataSource = dtEmp;
}

private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
{
//Get the column header cell bounds
Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

Point oPoint = new Point();

oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - ckBox.Width) + 1;
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - ckBox.Height) / 2 + 1;

if (oPoint.X + ckBox.Width < oRectangle.X)
{
ckBox.Visible = false;
}
else
{
ckBox.Visible = true;
}

//Change the location of the CheckBox to make it stay on the header
ckBox.Location = oPoint;
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == 0)
ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
}


Cheers.

No comments: