简体   繁体   中英

WinForm wrap text if current row width is filled

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:

在此输入图像描述 在此输入图像描述

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

在此输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM