none
Wrapping Text in a DataGridView Cell

    Question

  • I'm trying to get text in the cells of a DataGridView column to wrap.  I've read many posts on problems in this area and have tried many suggested solutions, and I am close, but it still doesn't quite work.  Where I am at now is that the text as it is being entered doesn't look like it is wrapping.  It is, but the cell is only displaying the last line.  After the end user is done if he clicks some other cell in the grid THEN the height of the row is suddenly adjusted so that all of the lines can be displayed (and they are). 

    I hope that I don't need to do anything fancy.  The code is shown below.  What's the easiest addition I can make to get the row height to adjust as the text is being entered so that all lines are visible.

     

    Here's the code.  (The DataGridView - dgvSource - is added via the designer, but all properties are set in the code here.)  It's the "Description" column which I would like to wrap.

    With dgvSource
    
          .ColumnCount = dgvSNumCols
          .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    
          '.Font = dgvFont
          With .ColumnHeadersDefaultCellStyle
            .BackColor = Color.Navy
            .ForeColor = Color.White
            .Font = New Font(.Font, FontStyle.Bold)
          End With
    
          .RowTemplate.Resizable = DataGridViewTriState.True
    
          .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
          .CellBorderStyle = DataGridViewCellBorderStyle.Single
          .GridColor = Color.Black
          .RowHeadersVisible = False
    
          .Anchor = CType(AnchorStyles.Bottom Or AnchorStyles.Right _
          Or AnchorStyles.Top Or AnchorStyles.Left, AnchorStyles)
    
          .AllowUserToAddRows = True
          .AllowUserToDeleteRows = True
          .AllowUserToOrderColumns = False
          .ReadOnly = False
    
          .ScrollBars = ScrollBars.Both
    
          .Columns(dgvSColCol).Name = "Col"
          .Columns(dgvSColCol).ValueType = GetType(Integer)
          .Columns(dgvSColCol).Width = 60
    
          .Columns(dgvSDescCol).Name = "Description"
          .Columns(dgvSDescCol).ValueType = GetType(String)
          .Columns(dgvSDescCol).Width = 140
          .Columns(dgvSDescCol).DefaultCellStyle.WrapMode = DataGridViewTriState.True
    
          .Columns(dgvSTargCol).Name = "Target"
          .Columns(dgvSTargCol).ValueType = GetType(Integer)
          .Columns(dgvSTargCol).Width = 60
    
          .MultiSelect = False
    
        End With
    

    I'd be grateful for any help.  This is driving me nuts.

    Thanks,  Bob

    Saturday, July 16, 2011 3:39 AM

Answers

  • Sorry, Bob. You must adjust it by yourself. The best way is to adjust it in the CellEndEdit event and if you want to adjust the row height, you will need to set the dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; Or you will failed to set it.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eBob.com Friday, July 22, 2011 5:53 PM
    Friday, July 22, 2011 7:27 AM

All replies

  • Hi Bob,

    Thanks for posting.

    I have moved this thread to Windows Forms General forum where you can get better answer.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 9:52 AM
    Moderator
  • Hi Bob,

    Welcome to the MSDN Forum.

    Here is thread which has meet the same problem as yours. You can use the answer to wrap the word in the content:

    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3d753f77-dc20-4acb-9c1a-f60d2f9f3a08/

    If you have any quesitons, please feel free to tell us.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 20, 2011 7:22 AM
  • I simplified the code to one column and I think that it follows the advice Neddy provided the link to.  But the height of the row does not adjust as the input is being typed.  Thus the end user can not see everything he has typed.  When ANOTHER cell is clicked then the height will adjust.  Here's the simple, one column code:

     

    With dgv
    
       .ColumnCount = 1
       .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
       With .ColumnHeadersDefaultCellStyle
        .Font = New Font(.Font, FontStyle.Bold)
       End With
    
       .RowTemplate.Resizable = DataGridViewTriState.True
    
       .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
       .CellBorderStyle = DataGridViewCellBorderStyle.Single
       .GridColor = Color.Black
       .RowHeadersVisible = False
    
       .Anchor = CType(AnchorStyles.Bottom Or AnchorStyles.Right _
       Or AnchorStyles.Top Or AnchorStyles.Left, AnchorStyles)
    
       .AllowUserToAddRows = True
       .AllowUserToDeleteRows = True
       .AllowUserToOrderColumns = False
       .ReadOnly = False
    
       .ScrollBars = ScrollBars.Both
    
       .Columns(0).Name = "Wrapped Text Column"
       .Columns(0).ValueType = GetType(String)
       .Columns(0).Width = 160
       .Columns(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True
       '.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
    
      End With
    
    

     


    BTW - with that last statement (.Columns(0).AutoSizeMode = ...) enabled the initial column width is way less than the specified 160 pixels.

    How do I get the column height to adjust as the end user is typing so that he can see everything he has typed?

    Thanks,  Bob

     

     


    Wednesday, July 20, 2011 2:27 PM
  • Hi Bob,

    Please have a try to use DataGridViewAutoSizeColumnMode enumeration, it will help you to show all the content in the DataGridViewCell.

    DataGridViewAutoSizeColumnMode Enumeration:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewautosizecolumnmode.aspx

    Such as:

     

     .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    

    If you have any questions, please feel free to tell us.

     

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, July 21, 2011 5:49 AM
  • Thanks Neddy.  BUT ... that adjusts the column width.  I want the row height to adjust.  AND ... like AutoSizeRows the adjustment does not happen while the end user is typing.  It takes a click in another cell before the adjustment happens.

    Thanks,  Bob

    Thursday, July 21, 2011 7:44 PM
  • Sorry, Bob. You must adjust it by yourself. The best way is to adjust it in the CellEndEdit event and if you want to adjust the row height, you will need to set the dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; Or you will failed to set it.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eBob.com Friday, July 22, 2011 5:53 PM
    Friday, July 22, 2011 7:27 AM
  • Thanks Neddy.

    Bob

     

    Friday, July 22, 2011 5:54 PM