Is it possible to have Multi-line DataGridView cells without wrapping text?

前端 未结 7 2100
暖寄归人
暖寄归人 2021-02-14 07:47

I know I can set WrapMode to true on the DefaultCellStyle of the RowTemplate, however this doesn\'t give me the behaviour I want. I am dis

7条回答
  •  没有蜡笔的小新
    2021-02-14 08:28

    It has worked for me by doing \r\n together with the string.

    For example "Hello" + \r\n. It then goes to the next row.

    EDIT:

    Just saw that this was WinForms. The trick above only works in WPF.

    EDIT2:

    You can use:

    dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    

    And if you don't want long items wrapping, you just do:

    String stringTest = "1234567891";
    if (stringTest.Length > 8)
    {
       stringTest = stringTest.Replace(stringTest.Substring(8), "...");
    }
    

    This will add "..." if the String is longer then 8.

提交回复
热议问题