简体   繁体   English

如果填充当前行宽,则WinForm换行文本

[英]WinForm wrap text if current row width is filled

I have DataGridView inside my WinForm. 我在WinForm中有DataGridView。 I set AutoSizeColumnsMode to Fill , because I need no white space if user resizes the window. 我将AutoSizeColumnsMode设置为Fill ,因为如果用户调整窗口大小,我不需要空格。

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: 我从其他SO答案中尝试过:

  • set AutoResizeRowsMode to AllCells - didn't help AutoResizeRowsMode设置为AllCells - 没有帮助
  • set DefaultCellStyle.WrapMode to True - ddin't help DefaultCellStyle.WrapMode设置为True - ddin't help

How can I achieve this actually? 我怎么能实现这个呢?

Edit: column settings: 编辑:列设置:

在此输入图像描述

Edit #2: 编辑#2:

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

Set RowsDefaultCellStyle.Wrapmode = true instead of DefaultCellStyle.WrapMode = true 设置RowsDefaultCellStyle.Wrapmode = true而不是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;
}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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