protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView == null)
            {
                return(new Size(-1, -1));
            }

            if (cellStyle == null)
            {
                throw new ArgumentNullException(nameof(cellStyle));
            }

            Size      preferredSize;
            Rectangle borderWidthsRect              = StdBorderWidths;
            int       borderAndPaddingWidths        = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights       = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            object formattedValue  = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize);
            string formattedString = formattedValue as string;

            if (string.IsNullOrEmpty(formattedString))
            {
                formattedString = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1)
            {
                switch (freeDimension)
                {
                case DataGridViewFreeDimension.Width:
                {
                    preferredSize = new Size(DataGridViewCell.MeasureTextWidth(graphics,
                                                                               formattedString,
                                                                               cellStyle.Font,
                                                                               Math.Max(1, constraintSize.Height - borderAndPaddingHeights - DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithWrapping - DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginBottom),
                                                                               flags),
                                             0);
                    break;
                }

                case DataGridViewFreeDimension.Height:
                {
                    preferredSize = new Size(0,
                                             DataGridViewCell.MeasureTextHeight(graphics,
                                                                                formattedString,
                                                                                cellStyle.Font,
                                                                                Math.Max(1, constraintSize.Width - borderAndPaddingWidths - DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft - DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginRight),
                                                                                flags));
                    break;
                }

                default:
                {
                    preferredSize = DataGridViewCell.MeasureTextPreferredSize(graphics,
                                                                              formattedString,
                                                                              cellStyle.Font,
                                                                              5.0F,
                                                                              flags);
                    break;
                }
                }
            }
            else
            {
                switch (freeDimension)
                {
                case DataGridViewFreeDimension.Width:
                {
                    preferredSize = new Size(DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Width,
                                             0);
                    break;
                }

                case DataGridViewFreeDimension.Height:
                {
                    preferredSize = new Size(0,
                                             DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Height);
                    break;
                }

                default:
                {
                    preferredSize = DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags);
                    break;
                }
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Height)
            {
                preferredSize.Width += DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft + DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginRight + borderAndPaddingWidths;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Width = Math.Max(preferredSize.Width, borderAndPaddingWidths + DATAGRIDVIEWCELL_iconMarginWidth * 2 + iconsWidth);
                }
            }
            if (freeDimension != DataGridViewFreeDimension.Width)
            {
                int verticalTextMarginTop = cellStyle.WrapMode == DataGridViewTriState.True ? DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithWrapping : DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithoutWrapping;
                preferredSize.Height += verticalTextMarginTop + DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginBottom + borderAndPaddingHeights;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Height = Math.Max(preferredSize.Height, borderAndPaddingHeights + DATAGRIDVIEWCELL_iconMarginHeight * 2 + iconsHeight);
                }
            }
            return(preferredSize);
        }
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            Size size;
            int  num3;
            int  num4;

            if (base.DataGridView == null)
            {
                return(new Size(-1, -1));
            }
            if (cellStyle == null)
            {
                throw new ArgumentNullException("cellStyle");
            }
            Rectangle stdBorderWidths = base.StdBorderWidths;
            int       num             = (stdBorderWidths.Left + stdBorderWidths.Width) + cellStyle.Padding.Horizontal;
            int       num2            = (stdBorderWidths.Top + stdBorderWidths.Height) + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimensionFromConstraint = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            string str = base.GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.PreferredSize | DataGridViewDataErrorContexts.Formatting) as string;

            if (string.IsNullOrEmpty(str))
            {
                str = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            if (base.DataGridView.ApplyVisualStylesToInnerCells)
            {
                Rectangle themeMargins = GetThemeMargins(graphics);
                num3 = themeMargins.X + themeMargins.Width;
                num4 = themeMargins.Y + themeMargins.Height;
            }
            else
            {
                num3 = num4 = 5;
            }
            switch (freeDimensionFromConstraint)
            {
            case DataGridViewFreeDimension.Height:
                if (((cellStyle.WrapMode != DataGridViewTriState.True) || (str.Length <= 1)) || ((((constraintSize.Width - num) - num3) - 4) <= 0))
                {
                    size = new Size(0, DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Height);
                    break;
                }
                size = new Size(0, DataGridViewCell.MeasureTextHeight(graphics, str, cellStyle.Font, ((constraintSize.Width - num) - num3) - 4, flags));
                break;

            case DataGridViewFreeDimension.Width:
                if (((cellStyle.WrapMode != DataGridViewTriState.True) || (str.Length <= 1)) || ((((constraintSize.Height - num2) - num4) - 2) <= 0))
                {
                    size = new Size(DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Width, 0);
                    break;
                }
                size = new Size(DataGridViewCell.MeasureTextWidth(graphics, str, cellStyle.Font, ((constraintSize.Height - num2) - num4) - 2, flags), 0);
                break;

            default:
                if ((cellStyle.WrapMode == DataGridViewTriState.True) && (str.Length > 1))
                {
                    size = DataGridViewCell.MeasureTextPreferredSize(graphics, str, cellStyle.Font, 5f, flags);
                }
                else
                {
                    size = DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags);
                }
                break;
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
            {
                size.Width += (num + num3) + 4;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Width = Math.Max(size.Width, (num + 8) + 12);
                }
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
            {
                size.Height += (num2 + num4) + 2;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Height = Math.Max(size.Height, (num2 + 8) + 11);
                }
            }
            return(size);
        }
        /// <include file='doc\DataGridViewUtilities.uex' path='docs/doc[@for="DataGridViewUtilities.GetPreferredRowHeaderSize"]/*' />
        internal static Size GetPreferredRowHeaderSize(Graphics graphics,
                                                       string val,
                                                       DataGridViewCellStyle cellStyle,
                                                       int borderAndPaddingWidths,
                                                       int borderAndPaddingHeights,
                                                       bool showRowErrors,
                                                       bool showGlyph,
                                                       Size constraintSize,
                                                       TextFormatFlags flags)
        {
            Size preferredSize;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);

            switch (freeDimension)
            {
            case DataGridViewFreeDimension.Width:
            {
                int preferredWidth = 0, allowedHeight = constraintSize.Height - borderAndPaddingHeights;
                if (!string.IsNullOrEmpty(val))
                {
                    int maxHeight = allowedHeight - 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                    if (maxHeight > 0)
                    {
                        if (cellStyle.WrapMode == DataGridViewTriState.True)
                        {
                            preferredWidth = DataGridViewCell.MeasureTextWidth(graphics, val, cellStyle.Font, maxHeight, flags);
                        }
                        else
                        {
                            preferredWidth = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Width;
                        }
                        preferredWidth += 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth + DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft + DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                    }
                }
                if (allowedHeight >= DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight)
                {
                    if (showGlyph)
                    {
                        preferredWidth += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                    }
                    if (showRowErrors)
                    {
                        preferredWidth += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                    }
                }
                preferredWidth  = Math.Max(preferredWidth, 1);
                preferredWidth += borderAndPaddingWidths;
                return(new Size(preferredWidth, 0));
            }

            case DataGridViewFreeDimension.Height:
            {
                int minHeightIcon = 1, minHeightContent = 1;
                int allowedWidth = constraintSize.Width - borderAndPaddingWidths;
                if (!string.IsNullOrEmpty(val))
                {
                    if (showGlyph && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        // There is enough room for the status icon
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                        // Status icon takes priority
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth;
                    }
                    if (showRowErrors && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        // There is enough room for the error icon
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                        // There is enough room for both the status and error icons
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth;
                    }
                    if (allowedWidth > 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight)
                    {
                        // There is enough room for text
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                        if (cellStyle.WrapMode == DataGridViewTriState.True)
                        {
                            minHeightContent = DataGridViewCell.MeasureTextHeight(graphics, val, cellStyle.Font, allowedWidth, flags);
                        }
                        else
                        {
                            minHeightContent = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Height;
                        }
                        minHeightContent += 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                    }
                }
                else
                {
                    if ((showGlyph || showRowErrors) && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                    }
                }
                return(new Size(0, Math.Max(minHeightIcon, minHeightContent) + borderAndPaddingHeights));
            }

            default:
            {
                if (!string.IsNullOrEmpty(val))
                {
                    if (cellStyle.WrapMode == DataGridViewTriState.True)
                    {
                        preferredSize = DataGridViewCell.MeasureTextPreferredSize(graphics, val, cellStyle.Font, 5.0F, flags);
                    }
                    else
                    {
                        preferredSize = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags);
                    }
                    preferredSize.Width += 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                                           DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                                           DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                    preferredSize.Height += 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                }
                else
                {
                    preferredSize = new Size(0, 1);
                }
                if (showGlyph)
                {
                    preferredSize.Width += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                }
                if (showRowErrors)
                {
                    preferredSize.Width += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                }
                if (showGlyph || showRowErrors)
                {
                    preferredSize.Height = Math.Max(preferredSize.Height,
                                                    DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight);
                }
                preferredSize.Width  += borderAndPaddingWidths;
                preferredSize.Height += borderAndPaddingHeights;
                return(preferredSize);
            }
            }
        }