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";
                    roTextBox.Invalidate();
                }
            }
            if (dgv.SummaryColumns != null && dgv.SummaryColumns.Length > 0 && sumBoxHash.Count > 0)
            {
                foreach (DataGridViewRow dgvRow in dgv.Rows)
                {
                    foreach (DataGridViewCell dgvCell in dgvRow.Cells)
                    {
                        foreach (DataGridViewColumn dgvColumn in sumBoxHash.Keys)
                        {
                            if (dgvCell.OwningColumn.Equals(dgvColumn))
                            {
                                ReadOnlyTextBox sumBox = (ReadOnlyTextBox)sumBoxHash[dgvColumn];

                                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>
        /// Resize the summary Boxes depending on the
        /// width of the Columns of the DataGridView
        /// </summary>
        private void resizeSumBoxes()
        {
            this.SuspendLayout();
            if (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 (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 = 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)
            {
                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;
                if (summaryRowBackColor == null)
                {
                    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;

                            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(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.Font      = new Font(dgv.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(this.InitialHeight - sumRowHeaderLabel.Height) / 2F);
                sumRowHeaderLabel.Text      = dgv.SumRowHeaderText;

                sumRowHeaderLabel.ForeColor = dgv.DefaultCellStyle.ForeColor;
                sumRowHeaderLabel.Margin    = new Padding(0);
                sumRowHeaderLabel.Padding   = new Padding(0);

                this.Controls.Add(sumRowHeaderLabel);
            }
            calcSummaries();
            resizeSumBoxes();
        }
Пример #5
0
        private void DataGridViewSummary_DataSourceChanged(object sender, EventArgs e)
        {
            SuspendLayout();
            if (firstBind)
            {
                firstBind = false;


                //  AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;//自动调整单元格大小适应内容
                SelectionMode = DataGridViewSelectionMode.FullRowSelect; //单击选择整行
                MultiSelect   = false;                                   //不允许多选单元格

                // ColumnHeadersHeight = cellFontSize+15;

                ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//列标题居中显示
                ColumnHeadersDefaultCellStyle.BackColor = Color.Silver;

                RowsDefaultCellStyle.BackColor            = Color.White;
                AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
                ColumnHeadersDefaultCellStyle.Font        = new System.Drawing.Font("宋体", headerFontSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                RowsDefaultCellStyle.Font = new Font("宋体", cellFontSize);
                AllowUserToResizeRows     = false;

                if (hasCheckBoxColumn)
                {
                    Columns[0].MinimumWidth = 50;
                    Columns[0].HeaderText   = "";
                    Columns[0].SortMode     = DataGridViewColumnSortMode.NotSortable;

                    int rowHeaderWidth = RowHeadersVisible ? RowHeadersWidth - 1 : 0;
                    int x = Columns[0].Width / 2 - headerCheckBox.Width / 2 + rowHeaderWidth;
                    int y = ColumnHeadersHeight / 2 - headerCheckBox.Height / 2;
                    headerCheckBox.Location = new Point(x, y);
                    headerVisibleX          = Columns[0].Width / 2;
                    headerCheckBox.Visible  = true;
                }
            }

            Graphics gh = this.CreateGraphics();
            SizeF    sf = gh.MeasureString(RowCount.ToString(), RowsDefaultCellStyle.Font);

            RowHeadersWidth = (int)sf.Width + 20;

            if (summaryRowVisible && summaryColumns != null)
            {
                foreach (DataGridViewColumn dgvColumn in Columns)
                {
                    if (summaryColumns.Contains(dgvColumn.Name))
                    {
                        ReadOnlyTextBox sumBox = (ReadOnlyTextBox)summaryControl.sumBoxHash[dgvColumn];

                        sf = gh.MeasureString(sumBox.Text, RowsDefaultCellStyle.Font);
                        dgvColumn.MinimumWidth = (int)sf.Width + 10;
                    }
                }
            }
            AutoResizeColumns();
            calculateColumnsWidth();
            ResumeLayout(false);
        }