locked
Wrapping the Data of Data Grid View column RRS feed

  • Question

  • Hi, how to wrap DataGrid View column data?Even though I set the WrapMode in DataGridViewCellStyle property of DataGridView to True,still wrapping is not happening.How to do this?Thanks in advance.





    cas

    Thursday, August 9, 2012 4:56 PM

Answers

  • Hi Cas,

    You need to set the WrapMode to DataGridViewTriState.True and AutoSizeRowsMode to DataGridViewAutoSizeRowsMode.AllCells.

    Check this sample.

    DataTable dt = new DataTable("dt");
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    
    dt.Rows.Add(1, "longColumn1 longColumn1 longColumn1 longColumn2long");
    dt.Rows.Add(2, "column2");
    dt.Rows.Add(3, "column3");
    
    dataGridView1.DataSource = dt;
    dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

    However, I think the wrap mod have some limitation. Although the MSDN document said that

     If WrapMode is True for a cell that contains text, the cell displays newline characters as line breaks, but also wraps any lines that exceed the width of the cell.

    It doesn't work as expected. If the text is a random text, you will find the wrap doesn't work. I would suggest you do this manually. For more details, see http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/181a1ec7-4a69-48a8-b93c-2152460c35b6

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us



    • Edited by Bob Wu-MT Friday, August 10, 2012 8:32 AM
    • Marked as answer by Bob Wu-MT Wednesday, August 22, 2012 7:30 AM
    Friday, August 10, 2012 8:31 AM

All replies