/// <summary> /// 显示统计信息 /// </summary> private void ShowSummaryTextInfo() { int icnt = this._SummaryContainer.Controls.Count; _FirstColSumTextType = SummaryTextType.None; foreach (Control ctr in this._SummaryContainer.Controls) { SummaryTextBox sumTextBox = ctr as SummaryTextBox; if (sumTextBox == null) { continue; } if (sumTextBox.Name == "RowsHeader_Label") { continue; } DataGridViewColumn currCell = this.GridViewColumns[sumTextBox.Name]; if (currCell == null) { continue; } sumTextBox.ForeColor = this._SummaryRowForeColor; sumTextBox.BackColor = this._SummaryRowBackColor; if (sumTextBox.IsHeaderLabel) {//如果SummaryTextBox的IsHeaderLabel为true则显示统计标题文本 sumTextBox.Text = this._SummaryHeaderText; sumTextBox.TextAlign = HorizontalAlignment.Center; sumTextBox.Font = new Font(this.DefaultCellStyle.Font, this._SummaryHeaderBold ? FontStyle.Bold : FontStyle.Regular); _FirstColSumTextType = SummaryTextType.Text; sumTextBox.Invalidate(); continue; } if (sumTextBox.IsSummary) {//如果是SummaryTextBox的IsSummary为true则计算对应列的合计 sumTextBox.Text = this.CalcSum(currCell); sumTextBox.FormatString = currCell.DefaultCellStyle.Format; sumTextBox.TextAlign = AligmentHelper.TranslateGridColumnAligment(currCell.DefaultCellStyle.Alignment); sumTextBox.Invalidate(); continue; } if (!sumTextBox.IsHeaderLabel && !sumTextBox.IsSummary) { sumTextBox.Text = ""; sumTextBox.Invalidate(); } } this._SummaryRowHeaderLabel.Text = "√"; //如果第一列是文本,则将统计文本设置到_SummaryRowHeaderLabel if (_FirstColSumTextType != SummaryTextType.Text) { this._SummaryRowHeaderLabel.Text = this._SummaryHeaderText; } this._SummaryRowHeaderLabel.TextAlign = HorizontalAlignment.Center; this._SummaryRowHeaderLabel.Font = new Font(this.DefaultCellStyle.Font, this._SummaryHeaderBold ? FontStyle.Bold : FontStyle.Regular); this._SummaryRowHeaderLabel.Invalidate(); }
/// <summary> /// 调整统计项SummaryTextBox尺寸 /// </summary> private void AdjustSummaryTextBoxWidth() { int rowHeaderWidth = this.RowHeadersVisible ? this.RowHeadersWidth - 1 : 0; int curPos = rowHeaderWidth; int labelWidth = 0; foreach (DataGridViewColumn col in this.GridViewColumns.Values) { SummaryTextBox sumTextBox = (SummaryTextBox)this._SummaryTextHashTable[col]; if (sumTextBox == null) { continue; } //计算统计头文本宽度 if (!sumTextBox.Visible) { labelWidth += col.Width; continue; } sumTextBox.BorderColor = this.GridColor; sumTextBox.BackColor = this._SummaryRowBackColor; if (!col.Visible) { sumTextBox.Visible = false; continue; } int startX = curPos; if (this.HorizontalScrollBar.Visible) { startX = curPos - this.HorizontalScrollingOffset; } int currWidth = col.Width; if (sumTextBox.IsHeaderLabel) { currWidth = labelWidth + col.Width; } if (startX < rowHeaderWidth) { currWidth -= rowHeaderWidth - startX; startX = rowHeaderWidth; } if (startX + currWidth > this.Width) { currWidth = this.Width - startX; } if (this.RightToLeft == RightToLeft.Yes) { startX = this.Width - startX - currWidth; } if (sumTextBox.Left != startX || sumTextBox.Width != currWidth) { sumTextBox.SetBounds(startX, 0, currWidth + 1, this.RowTemplate.Height); sumTextBox.BorderColor = this.GridColor; sumTextBox.Visible = true; if (this._SummaryContainer.Controls.ContainsKey(sumTextBox.Name)) { SummaryTextBox originalTextBox = this._SummaryContainer.Controls[sumTextBox.Name] as SummaryTextBox; originalTextBox.SetBounds(startX, 0, currWidth + 1, this.RowTemplate.Height); originalTextBox.Invalidate(); originalTextBox.BringToFront(); } else { this._SummaryContainer.Controls.Add(sumTextBox); } sumTextBox.BringToFront(); } if (sumTextBox.IsHeaderLabel) { curPos += labelWidth + col.Width; } else { curPos += col.Width; } sumTextBox.Invalidate(); } this._SummaryContainer.Refresh(); }