/// <summary> /// Initializes a new instance of the <see cref="CfbComboBox"/> custom control. /// </summary> public CfbComboBox() { _readOnlyTextBox = new ReadOnlyTextBox() { Location = this.Location, Size = this.Size, Visible = false }; }
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e) { ReadOnlyTextBox roTextBox = (ReadOnlyTextBox)sumBoxHash[dgv.Columns[e.ColumnIndex]]; if (roTextBox != null) { if (roTextBox.IsSummary) { calcSummaries(); } } }
/// <summary> /// Calculate the Sums of the summary columns 计算统计行数据总和 /// </summary> private void calcSummaries() { foreach (ReadOnlyTextBox roTextBox in sumBoxHash.Values) { if (roTextBox.IsSummary) { roTextBox.Tag = 0; roTextBox.Text = "0"; } } if (dgv.SummaryColumns != null && dgv.SummaryColumns.Length > 0 && sumBoxHash.Count > 0) { //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); //stopwatch.Start(); // 开始监视代码运行时间 ////...... //stopwatch.Stop(); // 停止监视 //TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间 foreach (DataGridViewColumn dgvColumn in sumBoxHash.Keys) { ReadOnlyTextBox sumBox = (ReadOnlyTextBox)sumBoxHash[dgvColumn]; for (int i = 0; i < dgv.Rows.Count; i++) { DataGridViewCell dgvCell = dgv.Rows[i].Cells[dgvColumn.Index]; if (sumBox != null && sumBox.IsSummary) { if (dgvCell.Value != null && !(dgvCell.Value is DBNull)) { if (IsInteger(dgvCell.Value)) { sumBox.Tag = Convert.ToInt64(sumBox.Tag) + Convert.ToInt64(dgvCell.Value); } else if (IsDecimal(dgvCell.Value)) { sumBox.Tag = Convert.ToDecimal(sumBox.Tag) + Convert.ToDecimal(dgvCell.Value); } sumBox.Text = string.Format("{0}", sumBox.Tag); sumBox.Invalidate(); } } } } } }
/// <summary> /// Create summary boxes for each Column of the DataGridView 为数据表各列创建统计文本 /// </summary> private void reCreateSumBoxes() { ReadOnlyTextBox sumBox; foreach (Control control in sumBoxHash.Values) { this.Controls.Remove(control); } sumBoxHash.Clear(); int iCnt = 0; List <DataGridViewColumn> sortedColumns = SortedColumns; foreach (DataGridViewColumn dgvColumn in sortedColumns) { sumBox = new ReadOnlyTextBox(); sumBoxHash.Add(dgvColumn, sumBox); sumBox.Top = 0; sumBox.Height = dgv.RowTemplate.Height; sumBox.BorderColor = dgv.GridColor; sumBox.ForeColor = Color.White; if (summaryRowBackColor == null || summaryRowBackColor == Color.Transparent) { sumBox.BackColor = dgv.DefaultCellStyle.BackColor; } else { sumBox.BackColor = summaryRowBackColor; } sumBox.BringToFront(); if (dgv.ColumnCount - iCnt == 1) { sumBox.IsLastColumn = true; } if (dgv.SummaryColumns != null && dgv.SummaryColumns.Length > 0) { for (int iCntX = 0; iCntX < dgv.SummaryColumns.Length; iCntX++) { if (dgv.SummaryColumns[iCntX] == dgvColumn.DataPropertyName || dgv.SummaryColumns[iCntX] == dgvColumn.Name) { dgvColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; sumBox.TextAlign = AlignmentTools.TranslateGridColumnAligment(dgvColumn.DefaultCellStyle.Alignment); sumBox.IsSummary = true; sumBox.FormatString = dgvColumn.CellTemplate.Style.Format; if (dgvColumn.ValueType == typeof(System.Int32) || dgvColumn.ValueType == typeof(System.Int16) || dgvColumn.ValueType == typeof(System.Int64) || dgvColumn.ValueType == typeof(System.Single) || dgvColumn.ValueType == typeof(System.Double) || dgvColumn.ValueType == typeof(System.Single) || dgvColumn.ValueType == typeof(System.Decimal)) { sumBox.Tag = System.Activator.CreateInstance(dgvColumn.ValueType); } } } } sumBox.BringToFront(); this.Controls.Add(sumBox); iCnt++; } if (dgv.DisplaySumRowHeader) { sumRowHeaderLabel.Visible = true; sumRowHeaderLabel.Text = dgv.SumRowHeaderText; sumRowHeaderLabel.ForeColor = dgv.RowHeadersDefaultCellStyle.ForeColor; sumRowHeaderLabel.Font = new Font(dgv.DefaultCellStyle.Font, dgv.SumRowHeaderTextBold ? FontStyle.Bold : FontStyle.Regular); sumRowHeaderLabel.Height = dgv.RowTemplate.Height; sumRowHeaderLabel.Width = dgv.RowHeadersWidth; } calcSummaries(); resizeSumBoxes(); }
/// <summary> /// Resize the summary Boxes (depending on the width of the Columns of the DataGridView) /// 调整统计文本框大小(取决于数据表的列宽) /// </summary> private void resizeSumBoxes() { this.SuspendLayout(); if (sumBoxHash != null && sumBoxHash.Count > 0) { try { int rowHeaderWidth = dgv.RowHeadersVisible ? dgv.RowHeadersWidth - 1 : 0; int sumLabelWidth = dgv.RowHeadersVisible ? dgv.RowHeadersWidth - 1 : 0; int curPos = rowHeaderWidth; if (dgv.DisplaySumRowHeader && sumLabelWidth > 0) { if (!sumRowHeaderLabel.Visible) { sumRowHeaderLabel.Visible = true; } sumRowHeaderLabel.Width = sumLabelWidth; if (dgv.RightToLeft == RightToLeft.Yes) { if (sumRowHeaderLabel.Dock != DockStyle.Right) { sumRowHeaderLabel.Dock = DockStyle.Right; } } else { if (sumRowHeaderLabel.Dock != DockStyle.Left) { sumRowHeaderLabel.Dock = DockStyle.Left; } } } else { if (sumRowHeaderLabel.Visible) { sumRowHeaderLabel.Visible = false; } } int iCnt = 0; Rectangle oldBounds; foreach (DataGridViewColumn dgvColumn in SortedColumns) //dgv.Columns) { ReadOnlyTextBox sumBox = (ReadOnlyTextBox)sumBoxHash[dgvColumn]; if (sumBox != null) { oldBounds = sumBox.Bounds; if (!dgvColumn.Visible) { sumBox.Visible = false; continue; } int from = dgvColumn.Frozen ? curPos : curPos - dgv.HorizontalScrollingOffset; int width = dgvColumn.Width + (iCnt == 0 ? 0 : 0); if (from < rowHeaderWidth) { width -= rowHeaderWidth - from; from = rowHeaderWidth; } if (from + width > this.Width) { width = this.Width - from; } if (width < 4) { if (sumBox.Visible) { sumBox.Visible = false; } } else { if (this.RightToLeft == RightToLeft.Yes) { from = this.Width - from - width; } if (sumBox.Left != from || sumBox.Width != width) { sumBox.SetBounds(from, 0, width, 0, BoundsSpecified.X | BoundsSpecified.Width); } if (!sumBox.Visible) { sumBox.Visible = true; } } curPos += dgvColumn.Width + (iCnt == 0 ? 0 : 0); if (oldBounds != sumBox.Bounds) { sumBox.Invalidate(); } } iCnt++; } } finally { this.ResumeLayout(); } } }
/// <summary> /// Create summary boxes for each Column of the DataGridView /// </summary> private void ReCreateSumBoxes() { ReadOnlyTextBox sumBox; foreach (Control control in _sumBoxHash.Values) { Controls.Remove(control); } _sumBoxHash.Clear(); var iCnt = 0; var sortedColumns = SortedColumns; foreach (var dgvColumn in sortedColumns) { sumBox = new ReadOnlyTextBox(); _sumBoxHash.Add(dgvColumn, sumBox); sumBox.Top = 0; sumBox.Height = _dgv.DataGridView.RowTemplate.Height; sumBox.BorderColor = _dgv.DataGridView.GridColor; if (SummaryRowBackColor == null) { sumBox.BackColor = _dgv.DataGridView.DefaultCellStyle.BackColor; } else { sumBox.BackColor = SummaryRowBackColor; } sumBox.BringToFront(); if (_dgv.DataGridView.ColumnCount - iCnt == 1) { sumBox.IsLastColumn = true; } if (_dgv.SummaryColumns != null && _dgv.SummaryColumns.Length > 0) { for (var iCntX = 0; iCntX < _dgv.SummaryColumns.Length; iCntX++) { if (_dgv.SummaryColumns[iCntX] == dgvColumn.DataPropertyName || _dgv.SummaryColumns[iCntX] == dgvColumn.Name) { dgvColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; dgvColumn.CellTemplate.Style.Format = _dgv.FormatString; sumBox.TextAlign = TextHelper.TranslateGridColumnAligment(dgvColumn.DefaultCellStyle.Alignment); sumBox.IsSummary = true; sumBox.FormatString = dgvColumn.CellTemplate.Style.Format; if (dgvColumn.ValueType == typeof(Int32) || dgvColumn.ValueType == typeof(Int16) || dgvColumn.ValueType == typeof(Int64) || dgvColumn.ValueType == typeof(Single) || dgvColumn.ValueType == typeof(Double) || dgvColumn.ValueType == typeof(Single) || dgvColumn.ValueType == typeof(Decimal)) { sumBox.Tag = Activator.CreateInstance(dgvColumn.ValueType); } } } } sumBox.BringToFront(); Controls.Add(sumBox); iCnt++; } if (_dgv.DisplaySumRowHeader) { _sumRowHeaderLabel.Font = new Font(_dgv.DataGridView.DefaultCellStyle.Font, _dgv.SumRowHeaderTextBold ? FontStyle.Bold : FontStyle.Regular); _sumRowHeaderLabel.Anchor = AnchorStyles.Left; _sumRowHeaderLabel.TextAlign = ContentAlignment.MiddleLeft; _sumRowHeaderLabel.Height = _sumRowHeaderLabel.Font.Height; _sumRowHeaderLabel.Top = Convert.ToInt32(Convert.ToDouble(InitialHeight - _sumRowHeaderLabel.Height) / 2F); _sumRowHeaderLabel.Text = _dgv.SumRowHeaderText; _sumRowHeaderLabel.ForeColor = _dgv.DataGridView.DefaultCellStyle.ForeColor; _sumRowHeaderLabel.Margin = new Padding(0); _sumRowHeaderLabel.Padding = new Padding(0); Controls.Add(_sumRowHeaderLabel); } CalcSummaries(); ResizeSumBoxes(); }