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:
AutoResizeRowsMode
to AllCells
- didn't help DefaultCellStyle.WrapMode
to True
- ddin't help How can I achieve this actually?
Edit: column settings:
Edit #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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.