Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView setting Row height in code and disable manual resize

In my grid I had following line of code which disabled user's manual resizing:

dgvTruckAvail.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

Now I needed to set column height in code and it didn't work (see DataGridView setting row height doesn't work)

I figured that it was this line of code that caused non-sizing issue. However, now I need to figure out how to

  1. Size rows in code

and

  1. Prevent user sizing rows themselves

Any pointers?

like image 854
katit Avatar asked Jan 02 '12 21:01

katit


3 Answers

Set:

dgvTruckAvail.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgvTruckAvail.AllowUserToResizeRows = false;

This will disable row autosizing and manual row resizing. To set the row height you can use the Height and MinimumHeight properties of the RowTemplate.

like image 56
Xint0 Avatar answered Nov 19 '22 07:11

Xint0


Setting row size is a real challenge. Check how many gripes and responses there are on the web. I've found that sometimes one way works and other times it no longer works.

Do this: Place a DataGridView on your form. Edit and add several columns. It doesn't matter what they are because you'll discard this DGV in a moment. Go into this DataGridView's properties and edit RowTemplate/Height to something small or large (only so you can see it working). Add a line of code in your method to set XXX.RowCount to something like 12 just to populate this test DataGridView. Run your code to verify the row height has changed.

Open the XXX.Designer.cs code. Expand the "Windows Form Designer generated code" and look for code that applies to what you just did with DataGridView. Copy it all into the method where you are trying to adjust/set row height. Comment out all of your code. Line by line, modify the self-generated code to use your object's name. Test repeatedly for any failure and isolate it at that time.

Now go back and delete this test DataGridView object.

like image 34
Col Tomb Avatar answered Nov 19 '22 07:11

Col Tomb


In the properties window set:

AllowUserToResizeRows = False

like image 45
Ed_ Avatar answered Nov 19 '22 05:11

Ed_