Adjust records height inside datagridview

ankit goel 746 Reputation points
2023-10-07T07:12:53.9733333+00:00

User's image

the above is a screenshot of my datagridview which has 20records . on initial display it is showing 17 records but the 17th record seems to cut off , how to fix this .

also in the same example , i have also used the below code to make the selected row's text bold and italic but seems that it also have a problem

   DataGridView dataGridView = sender as DataGridView;
            if (dataGridView.Rows[e.RowIndex].Selected)
            {
                // use both italic and bold
                e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold | FontStyle.Italic);

                // edit: to change the background color:
                //e.CellStyle.SelectionBackColor = Color.Coral;
            }


User's image

you can see that the fifth record is losing some of the text . What i am looking to modify my code in such that the long text should gets shrink to get into column1 of datagridview . please suggest how to fix the both .

thanks in advance

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,392 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,201 Reputation points
    2023-10-07T14:07:31.5066667+00:00

    I would not be concerned with a row not fully displaying. There are properties such as height to tweak of all rows.

    R1

    For the column issue, after finished populating the DataGridView the following extension will fix that.

    public static class DataGridViewExtensions
    {
        public static void ExpandColumns(this DataGridView source, bool sizable = false)
        {
            foreach (DataGridViewColumn col in source.Columns)
            {
                if (col.ValueType.Name != "ICollection`1")
                {
                    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
            }
    
            if (!sizable) return;
    
            for (int index = 0; index <= source.Columns.Count - 1; index++)
            {
                int columnWidth = source.Columns[index].Width;
    
                source.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    
                // Set Width to calculated AutoSize value:
                source.Columns[index].Width = columnWidth;
            }
    
    
        }
    }
    

    Usage

    dataGridView1.ExpandColumns();
    
    0 comments No comments

  2. KOZ6.0 5,215 Reputation points
    2023-10-07T19:13:39.7666667+00:00

    There's nothing you can do about the vertical cutoff problem if the size is variable. If the size is fixed, please adjust it so that this does not happen.

    To make the font smaller when the sides are cut off, customize DataGridViewTextBoxColumn as follows:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class DataGridViewTextBioxExColumn : DataGridViewTextBoxColumn
    {
        internal const char CTR_K = '\x0b';
    
        public DataGridViewTextBioxExColumn()  {
            base.CellTemplate = new DataGridViewTextBioxExCell();
        }
    }
    
    public class DataGridViewTextBioxExCell : DataGridViewTextBoxCell
    {
        protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex) {
            if (e.KeyChar == DataGridViewTextBioxExColumn.CTR_K) {
                Value = string.Empty;
                e.Handled = true;
            }
            base.OnKeyPress(e, rowIndex);
        }
    
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
            if (rowIndex >= 0 && !DataGridView.Rows[rowIndex].IsNewRow && !IsInEditMode) {
                Font prevFont = cellStyle.Font;
                string formattedString = formattedValue as string;
                Size ms = TextRenderer.MeasureText(graphics, formattedString, prevFont);
                const int padding = 4;
                int width = cellBounds.Width - padding;
                Font newFont = null;
                if (ms.Width > width) {
                    newFont = AdjustFont(graphics, formattedValue as string, width, prevFont);
                    cellStyle.Font = newFont;
                }
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                if (newFont != null) {
                    cellStyle.Font = prevFont;
                    newFont.Dispose();
                }
            } else {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            }
        }
    
        static Font AdjustFont(Graphics g, string formattedString, int width, Font font) {
            const int carve = 10;
            int min = carve;
            int max = (int)(font.Size * carve) - 1;
            int mid = (min + max) / 2;
            while (min + 1 < max) {
                using (Font newFont = new Font(font.FontFamily, mid / carve, font.Style)) {
                    Size ms = TextRenderer.MeasureText(g, formattedString, newFont);
                    if (ms.Width <= width) {
                        min = mid;
                    } else {
                        max = mid;
                    }
                }
                mid = (min + max) / 2;
            }
            return new Font(font.FontFamily, min / carve, font.Style);
        }
    
        public override Type EditType => typeof(DataGridViewTextBoxExEditingControl);
    }
    
    public class DataGridViewTextBoxExEditingControl : DataGridViewTextBoxEditingControl
    {
        protected override void OnKeyPress(KeyPressEventArgs e) {
            if (e.KeyChar == DataGridViewTextBioxExColumn.CTR_K) {
                Clear();
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }
    }
    

    enter image description here

    0 comments No comments