Beispiel #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.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)
                    {
                        //2012-08-21 by lin
                        //对于为什么要设置标题栏的Dock还没真正弄明白
                        //经过反复测试:不设置标签栏的Dock,对不是动态添加的GRID,会出现标签上下不居中
                        if (dgv.RightToLeft == RightToLeft.Yes)
                        {
                            if (sumRowHeaderLabel.Dock != DockStyle.Right)
                            {
                                sumRowHeaderLabel.Dock = DockStyle.Right;
                            }
                        }
                        else
                        {
                            if (sumRowHeaderLabel.Dock != DockStyle.Top)
                            {
                                sumRowHeaderLabel.Dock = DockStyle.Top;
                            }
                        }
                        if (!sumRowHeaderLabel.Visible)
                        {
                            sumRowHeaderLabel.Visible = true;
                        }
                    }
                    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();
                }
            }
        }