问题
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
toAllCells
- didn't help - set
DefaultCellStyle.WrapMode
toTrue
- 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