50

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.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
karthik
  • 4,485
  • 8
  • 30
  • 45

12 Answers12

68

You need to set the Height property of the RowTemplate:

var dgv = new DataGridView();
dgv.RowTemplate.Height = 30;
Rob
  • 45,296
  • 24
  • 122
  • 150
  • 1
    i set the property but height doesnt changed,any other property to be checked before doing this one – karthik Jul 30 '10 at 10:02
  • 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 =) – Rob Jul 30 '10 at 11:33
  • 2
    Thanks @Rob 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. http://stackoverflow.com/questions/8705312/datagridview-setting-row-height-doesnt-work. – Sami Sep 09 '12 at 15:18
  • 7
    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. – eddyparkinson Mar 04 '14 at 01:58
  • 1
    Keep in mind this property only works for Cell height, not ColumnHeader height. – iDillon May 19 '16 at 17:39
  • I had to do it in the designer (as stated by @Sebastian Brosch). – inliner49er Feb 10 '22 at 01:26
55

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.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
daniele3004
  • 13,072
  • 12
  • 67
  • 75
13

Try

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

I did that and it worked fine!

Ross
  • 1,313
  • 4
  • 16
  • 24
Charis
  • 131
  • 1
  • 2
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.

Arsalan
  • 709
  • 2
  • 14
  • 27
4

You also need to change the resizable property to true

    dataGridView1.RowTemplate.Resizable = DataGridViewTriState.True;
    dataGridView1.RowTemplate.Height = 50;
Mocas
  • 1,403
  • 13
  • 20
3

You need to :

dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Then :

dataGridView1.ColumnHeadersHeight = 60;
Nick
  • 1,178
  • 3
  • 24
  • 36
Billy Xd
  • 79
  • 5
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.

Raymond Dumalaog
  • 353
  • 4
  • 13
1

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;
   }
}
CedL
  • 113
  • 2
  • 13
0day.1337
  • 43
  • 7
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;
}
Tom
  • 11
  • 1
  • 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 [answer] and [edit] your answer to improve it. – Donald Duck Nov 06 '20 at 19:32
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 =)

juagicre
  • 1,065
  • 30
  • 42
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();

}
0

try simply:

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