Skip to main content

How to add select all checkbox to DataGridView


//Your DataGridView: dtgrd
//Flag to determine select all or not
bool bSelectAll = false;

if (dtgrd.Controls.Find("chkSelectAll", true).Length > 0)
{
dtgrd.Controls.RemoveByKey("chkSelectAll");
}
if (dtgrd.Rows.Count > 0)
{
DataGridViewCheckBoxColumn dtChkCol = new DataGridViewCheckBoxColumn(false);
dtChkCol.Width = 30;
dtChkCol.Resizable = DataGridViewTriState.False;
dtChkCol.ReadOnly = false;
dtgrd.Columns.Insert(0, dtChkCol);

Rectangle rect = dtgrd.GetCellDisplayRectangle(0, -1, true);
rect.X = rect.Location.X + (rect.Width / 3);
rect.Y = rect.Location.Y + (rect.Height / 3);
CheckBox chkSelectAll = new CheckBox();
chkSelectAll.Name = "chkSelectAll";
chkSelectAll.Size = new Size(18, 18);
chkSelectAll.Location = rect.Location;
chkSelectAll.CheckedChanged += new EventHandler(chkSelectAll_CheckedChanged);
chkSelectAll.Checked = false;
dtgrd.Controls.Add(chkSelectAll);

dtgrd.Columns[lcCheck].Frozen = true;
}


//Event here
private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
if (bSelectAll)
{
for (int i = 0; i < dtgrd.Rows.Count; i++)
{
dtgrd[lcCheck, i].Value = 0;
}
bSelectAll = false;
}
else
{
for (int i = 0; i < dtgrd.Rows.Count; i++)
{
dtgrd[lcCheck, i].Value = 1;
}
bSelectAll = true;
}
dtgrd.EndEdit();
}

Comments

Popular posts from this blog

pligg_validate does not return correct value

Pligg version 1.0.3 Before function pligg_validate(){ // returns the value for register validation return misc_validate; } After function pligg_validate(){ // returns the value for register validation global $db; $sql = "SELECT `var_value` FROM `" . table_config . "` WHERE `var_name` = '" . $db->escape('misc_validate') . "';"; $misc_validate = $db->get_var($sql); return return trim($misc_validate)=="true"?true:false; }

Wrong Referrer with Pligg

To debug the problem, edit /libs/html1.php, find the function check_referrer(). To fix this issue, replace $my_base_url value to ' http://localhost ' as the following code. $my_base_url = ' http://localhost '; $my_pligg_base = ''; $dblang = 'en'; define('table_prefix', 'pligg_'); $language = 'english'; include_once mnminclude.'settings_from_db.php';