コード例 #1
0
            /// <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();
                    }
                }
            }