Datagridview's row autoresize
Asked Answered
L

3

9

I'm trying to automatically adjust a row's height and I've found it very challenging.

I've already set this property :

DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells.

I've also made it using this other method:

DataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)

And also row by row, by using this:

DataGridView.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCells)

And also even hardcoded the row's height to a large value, and it didn't work either!! All the rows are shown with their defaults heights.

None of these worked. I'm running out of options.

Most of the rows in the datagridview don't need to be resized. But one of them is filled with values like these:

"a" + "\n" + b + "\n" + "c" + "\n" + "d" + "\n" + "e"

I mean, short values but in different lines. I have to show them in different lines, can't show them all together. But the datagridview shows only the first one and all the other ones are hidden, because the row doesn't autoresize.

Any idea about any other way to do it.

Lyte answered 23/11, 2012 at 15:45 Comment(0)
L
4

I've found the solution to this problem. Instead of working with the autosize properties of the grid or the rows, I should have used the wrapmode to be applied to all the grid's cells.

DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True

This is applied to all the cells and it works.

Lyte answered 23/11, 2012 at 18:41 Comment(2)
Glad you found the solution (I'd presumed you had set wrapmode already and were seeing the multiple lines overflowing the grid cell area). I'm going to leave my answer since it is correct for a different problem (if you update any of your cells increasing the length of the data, the resize won't trigger automatically).Storer
Oh - and mark this as the answer so the question drops off the unanswered list.Storer
S
5

You need to call the resize method after the data has been changed. From the MSDN article on DataGridView.AutoResizeRows():

The row heights are adjusted only once per method call; if the contents of the rows later change, the rows will not automatically adjust.

This means that you need to call the method after the first and any subsequent loading of the grid. If you are calling this code within the parent form's controller, databinding has not yet happened so the data is not there.

For the first loading using the DataBindingComplete event:

dataGridView1.DataBindingComplete += new
    DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
}

For later updates to cells you will need to find the best place yourself.

Storer answered 23/11, 2012 at 16:19 Comment(4)
The reason you current approach is not working is likely that you have your code within the forms constructor or the onload event.Storer
No, I actually do it after the gridsource has been set to the datagridview. It's already populated. It's the last thing I do.Lyte
@user1062568 when do you do it? In what event handler? even if you do it after the source has been set, that is not enough, you must do it after databinding is complete. If you set the source and then call resize all in the form constructor for example, this will not change the height.Storer
I was doing it after the grid was ready. I was actually adding row by row, using Rows.Add(). So, no binding was needed. Anyway, I've found the solution using another property. Thanks anyway for your help!Lyte
L
4

I've found the solution to this problem. Instead of working with the autosize properties of the grid or the rows, I should have used the wrapmode to be applied to all the grid's cells.

DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True

This is applied to all the cells and it works.

Lyte answered 23/11, 2012 at 18:41 Comment(2)
Glad you found the solution (I'd presumed you had set wrapmode already and were seeing the multiple lines overflowing the grid cell area). I'm going to leave my answer since it is correct for a different problem (if you update any of your cells increasing the length of the data, the resize won't trigger automatically).Storer
Oh - and mark this as the answer so the question drops off the unanswered list.Storer
R
0

What you are trying wont work. You need to set AutoSizeMode to 'none' then change the width of the column. It works even better if you set the columns manualy, since I assume you already know what columns you have.

Runyon answered 22/3, 2018 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.