DataGridView with Wrap text in Cell but without newline

ankit goel 766 Reputation points
2023-10-25T16:15:39.4733333+00:00

hi experts ,

I am trying to wrap the long text in my datagridview cell by using the

 dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;


but what it does is , it creates a newline in the cell itself to fill the rest of the text . i just want it to adjust all the text inside it according to the width of cell but should not create a new line or increase the height or width of the cell .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-10-27T00:19:55.9566667+00:00

    In the following code, there are two columns, the second after pasting text into it and leaving text will wrap.

    dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    dataGridView1.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    

    Can not help but should not increase the height or width of the cell .

    Screenshot after pasting in

    Method groups are not a concept that is preserved in compiled code - the compiler supports conversions from method groups to delegates - but in order for

    F1

    0 comments No comments

  2. KOZ6.0 6,735 Reputation points
    2023-10-27T00:26:54.94+00:00

    Part that draws the DataGridViewTextBoxCell.

    https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewTextBoxCell.cs#663

    The text drawing is below.

    https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewTextBoxCell.cs#770

    Click on a method to see how it works. Copy these to get the values of formattedString, valBounds, and flags.

    Size tmpSize = new Size(valBounds.Width, 1);
    Size size = TextRenderer.MeasureText(graphics, formattedString, font, tmpSize, flags);
    

    Executing will determine the size required to draw the text. Adjust the Font size so that this size is no larger than valBounds.Size.

    Just do your best with your own strength. I wish you good luck. ​

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.