DataGridView setting Row height in code and disable manual resize

﹥>﹥吖頭↗ 提交于 2019-12-30 02:45:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


In the properties window set:

AllowUserToResizeRows = False




回答4:


This works for me: 1. Open .Designer.cs (of form containing the gridview) 2. Back to gridview design and edit any property you want 3. Both tab will show * as of being modified 4. Done

I guest the modified status does not touch .Designer.cs file, so our modification in designer does not apply.



来源:https://stackoverflow.com/questions/8705640/datagridview-setting-row-height-in-code-and-disable-manual-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!