Reputation: 4735
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.
Upvotes: 53
Views: 191631
Reputation: 11
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();
}
Upvotes: 1
Reputation: 13910
You can set the row height by code:
dataGridView.RowTemplate.Height = 35;
Or in the property panel:
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.
Upvotes: 60
Reputation: 31
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 =)
Upvotes: 3
Reputation: 11
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;
}
Upvotes: 1
Reputation: 53
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;
}
}
Upvotes: 2
Reputation: 89
You need to :
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
Then :
dataGridView1.ColumnHeadersHeight = 60;
Upvotes: 3
Reputation: 1660
You also need to change the resizable property to true
dataGridView1.RowTemplate.Resizable = DataGridViewTriState.True;
dataGridView1.RowTemplate.Height = 50;
Upvotes: 4
Reputation: 353
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
.
Upvotes: 2
Reputation: 131
Try
datagridview.RowTemplate.MinimumHeight = 25;//25 is height.
I did that and it worked fine!
Upvotes: 13
Reputation: 744
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.
Upvotes: 8
Reputation: 45771
You need to set the Height
property of the RowTemplate:
var dgv = new DataGridView();
dgv.RowTemplate.Height = 30;
Upvotes: 72