/// <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();
            }
Example #2
0
        /// <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();
        }