Changing the row height of a DataGridView
Asked Answered
E

12

51

How can I change the row height of a DataGridView?

I set the value for the property but height doesn't change. Any other property has to be checked before setting this one.

Ealasaid answered 30/7, 2010 at 9:31 Comment(0)
K
71

You need to set the Height property of the RowTemplate:

var dgv = new DataGridView();
dgv.RowTemplate.Height = 30;
Kerbstone answered 30/7, 2010 at 9:38 Comment(7)
i set the property but height doesnt changed,any other property to be checked before doing this oneEalasaid
I'm not sure, you might want to check to see if the RowTemplate.Height property is being set elsewhere as this works correctly for me =)Kerbstone
Thanks @Kerbstone your guide worked for me. But I am surprised to see the conflict between this link and the question answered by you. Can you tell me the reason please. #8705812.Petry
For me, this fails with the first new row. But then starts to work, so the 2nd and 3rd rows have the new height. any ideas? I set the height in the designer, not in the code.Kienan
Keep in mind this property only works for Cell height, not ColumnHeader height.Hydrolyse
I had to do it in the designer (as stated by @Sebastian Brosch).Catamount
Must clear rows to have it effective. Like dgv.Rows.Clear(); dgv.RowTemplate.Height = 30;Lavatory
V
58

You can set the row height by code:

dataGridView.RowTemplate.Height = 35;

Or in the property panel:

enter image description here

Note the + sign to the left of the Row Template section name. You need to open it to see the Height field. By default it is closed.

Vocation answered 11/10, 2016 at 9:51 Comment(0)
H
13

Try

datagridview.RowTemplate.MinimumHeight = 25;//25 is height.

I did that and it worked fine!

Hanako answered 23/6, 2014 at 16:49 Comment(0)
G
8

you can do that on RowAdded Event :

_data_grid_view.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this._data_grid_view_RowsAdded);

private void _data_grid_view_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            _data_grid_view.Rows[e.RowIndex].Height = 42;
        }

when a row add to the dataGridView it just change it height to 42.

Gadolinite answered 13/5, 2014 at 7:59 Comment(0)
Y
4

You also need to change the resizable property to true

    dataGridView1.RowTemplate.Resizable = DataGridViewTriState.True;
    dataGridView1.RowTemplate.Height = 50;
Yeargain answered 1/6, 2017 at 16:26 Comment(0)
M
3

You need to :

dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Then :

dataGridView1.ColumnHeadersHeight = 60;
Massimo answered 12/2, 2018 at 13:55 Comment(0)
S
2

You can change the row height of the Datagridview in the .cs [Design].

Then click the datagridview Properties.

Look for RowTemplate and expand it,

then type the value in the Height.

Stiles answered 15/9, 2016 at 18:59 Comment(0)
N
2

What you have to do is to set the MinimumHeight property of the row. Not only the Height property. That's the key. Put the code bellow in the CellPainting event of the datagridview

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
   foreach(DataGridViewRow x in dataGridView1.Rows)
   {
     x.MinimumHeight = 50;
   }
}
Nutty answered 11/7, 2018 at 16:22 Comment(0)
B
1
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
    dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
}
Balaklava answered 6/11, 2020 at 18:25 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Consider reading How to Answer and edit your answer to improve it.Retrorse
Q
1

Make sure AutoSizeRowsMode is set to None else the row height won't matter because well... it'll auto-size the rows.

Should be an easy thing but I fought this for a few hours before I figured it out.

Better late than never to respond =)

Quietude answered 14/1, 2021 at 7:43 Comment(0)
O
1

this worked for me

int totalRowHeight = dataGridView1.ColumnHeadersHeight;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    totalRowHeight += row.Height;
}

if (totalRowHeight < dataGridView1.Height)
{
    totalRowHeight = dataGridView1.Height;
    totalRowHeight -= dataGridView1.ColumnHeadersHeight;
    int rowHeight = totalRowHeight / dataGridView1.Rows.Count;

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.MinimumHeight = rowHeight;
    }
    dataGridView1.Refresh();

}
Orphaorphan answered 14/9, 2022 at 9:35 Comment(0)
G
0

try simply:

dataGrid->Rows[i]->Height = 20;

Giesecke answered 4/10, 2022 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.