{"id":290,"date":"2014-08-30T16:56:04","date_gmt":"2014-08-30T07:56:04","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=290"},"modified":"2014-08-30T16:56:04","modified_gmt":"2014-08-30T07:56:04","slug":"gridheader-checkbox","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=290","title":{"rendered":"GridHeader CheckBox"},"content":{"rendered":"<p><a href=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2014\/08\/CheckBox.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2014\/08\/CheckBox.gif\" alt=\"CheckBox\" width=\"445\" height=\"228\" class=\"alignnone size-full wp-image-291\" \/><\/a><br \/>\n<pre id=\"code_snippet_75\" style=\"position:relative;width:100%;border:0;padding:0;\">private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)\r\n{\r\n    HeaderCheckBoxClick((CheckBox)sender);\r\n}\r\n\r\nprivate void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)\r\n{\r\n    if(e.KeyCode == Keys.Space)\r\n        HeaderCheckBoxClick((CheckBox)sender);\r\n}\r\n\r\nprivate void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)\r\n{\r\n    if (e.RowIndex == -1 &amp;&amp; e.ColumnIndex == 0)\r\n       ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);\r\n}\r\n\r\nprivate void AddHeaderCheckBox()\r\n{\r\n    HeaderCheckBox = new CheckBox();\r\n\r\n    HeaderCheckBox.Size = new Size(15, 15);\r\n\r\n    \/\/Add the CheckBox into the DataGridView\r\n    this.dgvSelectAll.Controls.Add(HeaderCheckBox);\r\n}\r\n\r\nprivate void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)\r\n{\r\n    \/\/Get the column header cell bounds\r\n    Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);\r\n\r\n    Point oPoint = new Point();\r\n\r\n    oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) \/ 2 + 1;\r\n    oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) \/ 2 + 1;\r\n\r\n    \/\/Change the location of the CheckBox to make it stay on the header\r\n    HeaderCheckBox.Location = oPoint;\r\n}\r\n\r\nprivate void HeaderCheckBoxClick(CheckBox HCheckBox)\r\n{\r\n    IsHeaderCheckBoxClicked = true;\r\n\r\n    foreach (DataGridViewRow Row in dgvSelectAll.Rows)\r\n        ((DataGridViewCheckBoxCell)Row.Cells[&quot;chkBxSelect&quot;]).Value = HCheckBox.Checked;\r\n\r\n    dgvSelectAll.RefreshEdit();\r\n\r\n    TotalCheckedCheckBoxes = HCheckBox.Checked ? TotalCheckBoxes : 0;\r\n\r\n    IsHeaderCheckBoxClicked = false;\r\n}\r\n\r\nprivate void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)\r\n{\r\n    if (RCheckBox != null)\r\n    {\r\n        \/\/Modifiy Counter;            \r\n        if ((bool)RCheckBox.Value &amp;&amp; TotalCheckedCheckBoxes &lt; TotalCheckBoxes)\r\n            TotalCheckedCheckBoxes++;\r\n        else if (TotalCheckedCheckBoxes &gt; 0)\r\n            TotalCheckedCheckBoxes--;\r\n\r\n        \/\/Change state of the header CheckBox.\r\n        if (TotalCheckedCheckBoxes &lt; TotalCheckBoxes)\r\n            HeaderCheckBox.Checked = false;\r\n        else if (TotalCheckedCheckBoxes == TotalCheckBoxes)\r\n            HeaderCheckBox.Checked = true;\r\n    }\r\n}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-c-form"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}