private static void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox) { if ((RCheckBox != null) && (RCheckBox.DataGridView is DataGridViewCust)) { DataGridViewCust dgvC = RCheckBox.DataGridView as DataGridViewCust; //Modifiy Counter; foreach (CheckColumTag cct in dgvC.PV_checkColumnsTag) { if (cct.PV_ColumnIndex == RCheckBox.ColumnIndex) { if ((bool)(RCheckBox.Value.Equals(RCheckBox.TrueValue)) && (cct.PV_TotalCheckedCheckBoxes < dgvC.RowCount)) { cct.PV_TotalCheckedCheckBoxes++; } else if (cct.PV_TotalCheckedCheckBoxes > 0) { cct.PV_TotalCheckedCheckBoxes--; } //Change state of the header CheckBox. if (cct.PV_TotalCheckedCheckBoxes < dgvC.RowCount) { cct.HeaderCheckBox.Checked = false; } else if (cct.PV_TotalCheckedCheckBoxes == dgvC.RowCount) { cct.HeaderCheckBox.Checked = true; } break; } } } }
/// <summary> /// 根据逻辑是否的值来设定值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void dataGridView_ContactCustDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (!(sender is DataGridViewCust)) { return; } DataGridViewCheckBoxCell lvc = null; DataGridViewCell lc = null; DataGridViewCust dgvC = sender as DataGridViewCust; foreach (CheckColumTag lvcct in dgvC.PV_checkColumnsTag) { if (e.ColumnIndex == lvcct.PV_ColumnIndex) { lc = dgvC.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (lc is DataGridViewCheckBoxCell) { lvc = lc as DataGridViewCheckBoxCell; if (lvc.Value != null) { if (lvc.ValueType != typeof(System.Boolean)) { e.Value = (lvc.Value.ToString() == lvcct.TrueValue.ToString()); } } } break; } } }
private static void HeaderCheckBoxClick(CheckBox HCheckBox) { if (HCheckBox.Tag == null) { return; } DataGridViewCust dgvC = HCheckBox.Tag as DataGridViewCust; dgvC.PV_IsHeaderCheckBoxClicked = true; DataGridViewCheckBoxCell dgvcbc = null; foreach (CheckColumTag cct in dgvC.PV_checkColumnsTag) { if (dgvC.Columns[cct.PV_ColumnIndex].ReadOnly) { continue; } if (Object.ReferenceEquals(cct.HeaderCheckBox, HCheckBox)) { foreach (DataGridViewRow Row in dgvC.Rows) { dgvcbc = Row.Cells[cct.PV_ColumnIndex] as DataGridViewCheckBoxCell; dgvcbc.Value = HCheckBox.Checked ? dgvcbc.TrueValue : dgvcbc.FalseValue; } cct.PV_TotalCheckedCheckBoxes = HCheckBox.Checked ? dgvC.RowCount : 0; } } dgvC.EndEdit(); dgvC.PV_IsHeaderCheckBoxClicked = false; }
void DataGridFooter_ParentChanged(object sender, EventArgs e) { if (this.Parent != null && !(this.Parent is DataGridViewCust)) { throw new Exception("不能将DataGridView添加到非DataGridViewCust控件之中"); } pv_DataGridViewCust = (DataGridViewCust)this.Parent; }
/// <summary> /// check值变化,需要自行提交 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (!(sender is DataGridViewCust)) { return; } DataGridViewCust dgvC = sender as DataGridViewCust; if (dgvC.CurrentCell is DataGridViewCheckBoxCell) { DataGridViewCheckBoxCell cell = dgvC.CurrentCell as DataGridViewCheckBoxCell; dgvC.CommitEdit(DataGridViewDataErrorContexts.Commit); } }
private static void dgvSelectAll_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) { if (!(sender is DataGridViewCust)) { return; } DataGridViewCust dgvC = sender as DataGridViewCust; foreach (CheckColumTag lvcct in dgvC.PV_checkColumnsTag) { //if (e.Column.Index == lvcct.PV_ColumnIndex) //{ ResetHeaderCheckBoxLocation(dgvC, lvcct.HeaderCheckBox, lvcct.PV_ColumnIndex); //} } }
/// <summary> /// 标题check位置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if ((e.RowIndex == -1) && (e.ColumnIndex >= 0)) { if (!(sender is DataGridViewCust)) { return; } DataGridViewCust dgvC = sender as DataGridViewCust; dgvC.SuspendLayout(); foreach (CheckColumTag lvcct in dgvC.PV_checkColumnsTag) { if (e.ColumnIndex == lvcct.PV_ColumnIndex) { ResetHeaderCheckBoxLocation(dgvC, lvcct.HeaderCheckBox, lvcct.PV_ColumnIndex); } } dgvC.ResumeLayout(); } }
//public string RowHeaderText //{ // get { return this._LeftView.Items[0].SubItems[0].Text; } // set // { // this._LeftView.Items[0].SubItems[0].Text = value; // this._RightView.Items[0].SubItems[0].Text = value; // } //} //public int RowHeaderWidth //{ // get { return this._LeftView.Columns[0].Width; } // set // { // this._LeftView.Columns[0].Width = value; // this._RightView.Columns[0].Width = value; // } //} /// <summary> /// 构造汇总区时,要传入被汇总的gridview /// </summary> /// <param name="dgvGroup"></param> public DataGridViewFooter(DataGridViewCust dgvGroup) : base() { this.pv_DataGridViewCust = dgvGroup; //this.Columns = new DataGridFooterColumnCollection(); //this.Visible = false; //this._ImgList.ImageSize = new Size(1, 18); //this.Columns = new DataGridFooterColumnCollection(this); this.BorderStyle = BorderStyle.None; this.pv_PanelFrozen.BorderStyle = BorderStyle.None; this.pv_PanelFrozen.BackColor = SystemColors.GradientActiveCaption; this.pv_PanelScroll.BorderStyle = BorderStyle.None; this.pv_PanelScroll.BackColor = SystemColors.AppWorkspace; //this._LeftView.BorderStyle = BorderStyle.None; //this._RightView.BorderStyle = BorderStyle.None; this.SuspendLayout(); Graphics g = this.CreateGraphics(); SizeF stringSize = new SizeF(); stringSize = g.MeasureString("合计", this.Font); this.Height = (int)stringSize.Height + 2; // //pv_PanelFrozen // this.pv_PanelFrozen.Margin = new Padding(0); this.pv_PanelFrozen.Height = this.Height; // //pv_PanelScroll // this.pv_PanelScroll.Margin = new Padding(0); this.pv_PanelScroll.Height = this.Height; this.Controls.Add(pv_PanelFrozen); this.Controls.Add(pv_PanelScroll); this.ResumeLayout(false); this.ParentChanged += new EventHandler(DataGridFooter_ParentChanged); }
/// <summary> /// 选择一行check /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void dgvSelectAll_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (!(sender is DataGridViewCust)) { return; } DataGridViewCust dgvC = sender as DataGridViewCust; if (e.RowIndex < 0) { return; } if (!dgvC.PV_IsHeaderCheckBoxClicked) { if (dgvC[e.ColumnIndex, e.RowIndex] is DataGridViewCheckBoxCell) { RowCheckBoxClick((DataGridViewCheckBoxCell)dgvC[e.ColumnIndex, e.RowIndex]); } //dgvC.EndEdit();堆栈溢出 若要更新怎么办? 溢出原因,因为true false对应的值跟数据源不匹配。导致点check无效,引起check值变化 又回来这个过程。所以,仅仅用标题栏的checkbox后 } }
private static void ResetHeaderCheckBoxLocation(DataGridViewCust dgvC, CheckBox HeaderCheckBox, int ColumnIndex) { //Get the column header cell bounds Rectangle oRectangle = dgvC.GetCellDisplayRectangle(ColumnIndex, -1, true); HeaderCheckBox.Height = oRectangle.Height - 4; if (oRectangle.Width > 4) { HeaderCheckBox.Visible = (dgvC.Columns[ColumnIndex].ReadOnly != true) && (dgvC.ReadOnly != true);; HeaderCheckBox.Width = oRectangle.Width - 4; } else { HeaderCheckBox.Visible = false; } //HeaderCheckBox.Location = oRectangle.Location; Point oPoint = new Point(); oPoint.X = oRectangle.Location.X + 2; oPoint.Y = oRectangle.Location.Y + 1; ////Change the location of the CheckBox to make it stay on the header HeaderCheckBox.Location = oPoint; }