1

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:

enter image description here

Edit #2:

enter image description here enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Tarasovych
  • 2,228
  • 3
  • 19
  • 51

2 Answers2

2

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

enter image description here

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

enter image description here

This is the code

enter image description here

HaPhan
  • 301
  • 1
  • 11
1

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;
}

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I've tested it in `Load` nethod, as you suggested. Still have a problem( – Tarasovych Sep 01 '18 at 10:05
  • Maybe the problem is my columns are predefined? And in your example they are not – Tarasovych Sep 01 '18 at 10:12
  • The only difference that a predefined column have with a new column, is in the settings which is applied on the column. You always need to test the solutions which are shared by you, in a clean environment. See the screen capture, it's working as expected. – Reza Aghaei Sep 01 '18 at 10:14
  • Take a look please https://streamable.com/9b5s6 (tested with your code) – Tarasovych Sep 01 '18 at 10:16
  • In your example, try to type a few words (Separated by space) to see the result. In your screen capture, you have a single long word and the behavior of the column is expected. – Reza Aghaei Sep 01 '18 at 10:22
  • Oh, my bad, sorry – Tarasovych Sep 01 '18 at 10:24
  • 1
    No worries, for any reason if you would like to customize rendering of the cell, if the requirement cannot be satisfied by changing the properties, you can handle `CellPainting` event and render the cell based on your custom logic. You can find an [example here](https://stackoverflow.com/q/50584362/3110834). – Reza Aghaei Sep 01 '18 at 12:27