Esempio n. 1
0
        /// <summary>
        /// 列合计计算
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        private string CalcSum(DataGridViewColumn col)
        {
            SummaryTextBox sumTextBox = this._SummaryTextHashTable[col] as SummaryTextBox;
            object         valObj     = sumTextBox.Tag;

            foreach (DataGridViewRow dgvRow in this.Rows)
            {
                if (this._SummaryTextHashTable.ContainsKey(col))
                {
                    if (sumTextBox != null && sumTextBox.IsSummary)
                    {
                        if (valObj == null)
                        {
                            valObj = 0;
                        }
                        if (dgvRow.Cells[col.Index].Value != null &&
                            !(dgvRow.Cells[col.Index].Value is DBNull))
                        {
                            if (IsInteger(dgvRow.Cells[col.Index].Value))
                            {
                                valObj = Convert.ToInt64(valObj) + Convert.ToInt64(dgvRow.Cells[col.Index].Value == null || dgvRow.Cells[col.Index].Value.ToString() == "" ? 0 : dgvRow.Cells[col.Index].Value);
                            }
                            else if (IsDecimal(dgvRow.Cells[col.Index].Value))
                            {
                                valObj = Convert.ToDecimal(valObj) + Convert.ToDecimal(dgvRow.Cells[col.Index].Value == null || dgvRow.Cells[col.Index].Value.ToString() == "" ? 0 : dgvRow.Cells[col.Index].Value);
                            }
                        }
                    }
                }
            }
            return(string.Format("{0}", valObj));
        }
Esempio n. 2
0
        /// <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();
        }
Esempio n. 3
0
        /// <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();
        }
Esempio n. 4
0
        private void RefreshSummaryTextBoxCache()
        {
            if (this._SummaryColumns == null || this._SummaryColumns.Length == 0)
            {
                this._SummaryContainer.Visible = false;
                return;
            }
            this._SummaryContainer.Visible = this._SummaryRowVisible;
            //统计列
            for (int i = 0; i < this._SummaryColumns.Length; i++)
            {
                if (this.GridViewColumns.ContainsKey(_SummaryColumns[i]))
                {
                    SummaryTextBox     sumTextBox = new SummaryTextBox();
                    DataGridViewColumn currCol    = this.GridViewColumns[_SummaryColumns[i]];
                    sumTextBox.Name          = currCol.DataPropertyName.Trim() == "" ? currCol.Name : currCol.DataPropertyName;
                    sumTextBox.IsSummary     = true;
                    sumTextBox.IsHeaderLabel = false;
                    if (!this._SummaryTextHashTable.ContainsKey(currCol))
                    {
                        this._SummaryTextHashTable.Add(currCol, sumTextBox);
                    }
                    if (currCol.DisplayIndex == 0)
                    {
                        _FirstColSumTextType = SummaryTextType.Number;
                    }
                }
            }

            //非统计列
            foreach (DataGridViewColumn currCol in this.GridViewColumns.Values)
            {
                if (!_SummaryTextHashTable.ContainsKey(currCol))
                {
                    SummaryTextBox sumTextBox = new SummaryTextBox();
                    sumTextBox.Name          = currCol.DataPropertyName.Trim() == "" ? currCol.Name : currCol.DataPropertyName;
                    sumTextBox.IsSummary     = false;
                    sumTextBox.IsHeaderLabel = false;
                    this._SummaryTextHashTable.Add(currCol, sumTextBox);
                }
            }

            foreach (DataGridViewColumn currCol in this.GridViewColumns.Values)
            {
                if (_FirstColSumTextType == SummaryTextType.None)
                {
                    //取得当前列的下一列
                    DataGridViewColumn NextCol = this.Columns.GetNextColumn(currCol, DataGridViewElementStates.None, DataGridViewElementStates.None);
                    if (NextCol == null)
                    {
                        break;
                    }
                    if (NextCol.Name.Trim() == "")
                    {
                        continue;
                    }
                    SummaryTextBox nextSumTextBox = this._SummaryTextHashTable[NextCol] as SummaryTextBox;
                    SummaryTextBox currSumTextBox = this._SummaryTextHashTable[currCol] as SummaryTextBox;
                    currSumTextBox.Name = currCol.DataPropertyName.Trim() == "" ? currCol.Name : currCol.DataPropertyName;

                    //如果下一列对应是SummaryTextBox实例是合计,且第一列不是头列时设置第一列为头列类型
                    if (nextSumTextBox.IsSummary && _FirstColSumTextType != SummaryTextType.Text)
                    {
                        currSumTextBox.IsHeaderLabel = true;
                        currSumTextBox.IsSummary     = false;
                        currSumTextBox.Visible       = true;
                        _FirstColSumTextType         = SummaryTextType.Text;
                    }
                    else
                    {
                        currSumTextBox.IsHeaderLabel = false;
                        currSumTextBox.IsSummary     = false;
                        currSumTextBox.Visible       = false;
                    }
                }
            }
        }