WinForm wrap text if current row width is filled

安稳与你 提交于 2020-01-05 08:43:12

问题


I have DataGridView inside my WinForm. I set AutoSizeColumnsMode to Fill, because I need no white space if user resizes the window.

What I want to achieve. For example, user types some text in the certain cell. When text width becomes as cell width, text must go one the new line.
How it might be:

|Cell Header|
-------------
|text-text  |

and

|Cell Header|
-------------
|text-text  |
|more text  |
|on the new |
|line       |

My columns are resizable by user. So this:

|Cell Header|
-------------
|text-text  |
|more text  |
|on the new |
|line       |

might go this:

|Cell Header        |
---------------------
|text-text more text|
|on the new line    |

What I have tried from other SO answers:

  • set AutoResizeRowsMode to AllCells - didn't help
  • set DefaultCellStyle.WrapMode to True - ddin't help

How can I achieve this actually?

Edit: column settings:

Edit #2:


回答1:


Set RowsDefaultCellStyle.Wrapmode = true instead of DefaultCellStyle.WrapMode = true

I used default settings for both of the grid. This is my settings :

This is the code




回答2:


In addition to setting wrap mode and auto size mode for the column, you need to set the size mode for rows as well.

public class Model
{
    public int Id { get; set; }
    public string Text { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
    var list = new List<Model>
    {
        new Model{Id = 1, Text = "Lorem ipsum" },
        new Model{Id = 2, Text = "Lorem ipsum dolor sit amet." },
        new Model{Id = 3, Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." },
    };
    dataGridView1.DataSource = list;
    dataGridView1.Columns["Text"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    dataGridView1.Columns["Text"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}



来源:https://stackoverflow.com/questions/52126413/winform-wrap-text-if-current-row-width-is-filled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!