Click here to Skip to main content
11,920,156 members (57,609 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# DataGrid
I am using the code found here to print my windows form datagrid. My question that I have is how can I make the row height greater than the default height?

It doesn't look like it showed my link, so here it is:
Another DataGridView Printer[^]
Posted 2-Apr-13 9:02am
Edited 2-Apr-13 9:57am
Kenneth Haugland 2-Apr-13 14:50pm
And this is WinForms or?
FoxTrotCharlie 2-Apr-13 14:56pm
Sorry, yes this is for a windows form.
Kenneth Haugland 2-Apr-13 14:57pm
Well, cant help you there, but fixed the tag for you :-)
FoxTrotCharlie 2-Apr-13 14:59pm
Apologies for the incorrect tag, but thank you for updating!
Kenneth Haugland 2-Apr-13 15:02pm
No problem. I could help you in WPF but not WinForms. I skipped WinForms and went straight for WPF.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It seems to be simple to achieve:
printer.RowHeight = DGVPrinter.RowHeightSetting.CellHeight;

Please, follow the link to the article, go to the end of it to find Comments and Discussion section. There ask a question Wink | ;)

You can use DataGridViewRow.Height[^] or DataGridViewRow.MinimumHeight[^] property too.

DataGridViewRow row = dataGridView.Rows[0];
row.Height = 15;
DataGridViewRow row = dataGridView.Rows[1];
row.MinimumHeight = 40;
FoxTrotCharlie 2-Apr-13 15:40pm
I saw that piece, but I thought I would have to actually set the height. Like CellHeight = 32;

Should I close this question and post in the QNA of the thread I linked back to? Is that the proper protocol?
Maciej Los 2-Apr-13 15:47pm
Not necessary, but it's better to ask author of article...
See my updated answer.
FoxTrotCharlie 2-Apr-13 15:58pm
Does that element of coding go in my DataGrid load event?

When I attempt to use my datagrid I get a compile error of dgvScheduler is a 'field' but is used like a 'type'

This is the syntax I used:
dgvScheduler row = dgvScheduler.Rows[0];
row.Height = 15;
Maciej Los 2-Apr-13 16:09pm
NOT dgvScheduler row = dgvScheduler.Rows[0], but DataGridViewRow row = dgvScheduler.Rows[0]
Do you see the difference?
FoxTrotCharlie 2-Apr-13 16:27pm
I do see the difference. Thank you for pointing that out. I get a debug error of Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
Maciej Los 2-Apr-13 16:35pm
Ok, create new event for DataGridView: DataSourceChanged[^] and than place this code:
foreach (DataGridViewRow row in DataGridView1.Rows)
row.Height = 15;
FoxTrotCharlie 2-Apr-13 16:47pm
I added this code:
private void dgvScheduler_DataSourceChanged(object sender, DataGridViewCellEventArgs e)
foreach (DataGridViewRow row in dgvScheduler.Rows)
row.Height = 15;

but this is not making a change when I load the datagrid. Did I miss a step you listed out for me to follow?

EDIT ---
When stepping through my code it looks like this DataSourceChanged event for my DataGrid is not actually hit?!
Maciej Los 2-Apr-13 17:32pm
Default row's height is bigger than 15 ;)
Debug program to check it ;)
FoxTrotCharlie 2-Apr-13 21:56pm
I tried all kinds of sizes, I set it to 600, 6000 also and still no change.

When debugging my code never hits the dgvScheduler_DataSourceChnaged event! The way my form works is it loads a form with a blank datagrid. I press the load data button & a inputbox pops up asking who to load data on, once that selection has been made the grid is then populated. There is a separate button to press if the user desires to print the grid.

I can post code if necessary, since my code doesn't hit the DataSourceChanged.
Maciej Los 3-Apr-13 1:35am
Weird behavior...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I actually just discovered how to set the row height for a datagrid. I used this code:

dgvScheduler.RowTemplate.MinimumHeight = 30;
//And just set the Height to whatever value you are after!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100