/// <summary> /// Handles the Load event of the FuncCodePopupForm control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void FuncCodePopupForm_Load(object sender, EventArgs e) { OKBtn.DialogResult = System.Windows.Forms.DialogResult.OK; FuncCodeDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleTurquoise;//奇數列顏色 DataGridViewCheckBoxColumn cbCol = new DataGridViewCheckBoxColumn(); cbCol.Name = "ck"; cbCol.Width = 50; //設定寬度 //cbCol.HeaderText = " Select All"; cbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //置中 FuncCodeDataGridView.Columns.Insert(0, cbCol); #region 建立全選 CheckBox //建立個矩形,等下計算 CheckBox 嵌入 GridView 的位置 Rectangle rect = FuncCodeDataGridView.GetCellDisplayRectangle(0, -1, true); rect.X = rect.Location.X + rect.Width / 4 - 9; rect.Y = rect.Location.Y + (rect.Height / 2 - 9); CheckBox cbHeader = new CheckBox(); cbHeader.Name = "checkboxHeader"; cbHeader.Size = new Size(18, 18); cbHeader.Location = rect.Location; //全選要設定的事件 cbHeader.CheckedChanged += new EventHandler(cbHeader_CheckedChanged); //將 CheckBox 加入到 dataGridView FuncCodeDataGridView.Controls.Add(cbHeader); #endregion loadResiteredFunc(); }
/// <summary> /// Handles the CheckedChanged event of the cbHeader control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void cbHeader_CheckedChanged(object sender, EventArgs e) { foreach (DataGridViewRow dr in FuncCodeDataGridView.Rows) { dr.Cells["ck"].Value = ((CheckBox)FuncCodeDataGridView.Controls.Find("checkboxHeader", true)[0]).Checked; } FuncCodeDataGridView.EndEdit(); }