DataGridView resizeable columns, but last column can't be resized bigger?
Asked Answered
U

6

6
For c = 0 To grd.Columns.Count - 1
    grd.Columns(c).Resizable = DataGridViewTriState.True
Next c

That allows all the columns in my DataGridView to be resizable EXCEPT the last column. Why? I DO get the little resizable-mouse-pointer... but you can only make the column SMALLER, never WIDER.

(Same problem if I try to set all the columns to Resizable, using the VB.net IDE)

What am I doing wrong here? Shouldn't I be able to resize ANY column I want, bigger or smaller?

Upholstery answered 10/5, 2011 at 14:10 Comment(0)
D
0

You can't make the last column wider because it's already filling all of the available space in the control!

Unlike the other columns in the middle, which simply steal space from the column to their right, there is no column to the right of the last column for it to steal space from. Since it doesn't have anywhere to go, the control simply prevents you from resizing the last column.

Disc answered 10/5, 2011 at 14:17 Comment(1)
Every other grid we've ever seen... you simply grab ANY column header (including the last one)... and slide it right. We aren't trying to make the GRID wider... or make the column extent beyond the WIDTH of the grid.... just trying to make the COLUMN wider. I thought that's why there's a horizontal scrollbar at the bottom. No?Upholstery
G
0

I've had the same problem - I couldn't resize the last column in DataGridView (the resizable property was set to 'True'). I've had 'Fill mode' set to 'Displayed Cells'. When I've changed that setting to 'Not set', I can normally resize the last column.

Gaucherie answered 26/1, 2014 at 7:45 Comment(1)
I think you're missing the point of the last column not being able to resize to a larger size because the grid control itself ends there and there is no space available to resize it to.Boanerges
P
0

What you probably want is for the user to resize the left edge of last column, not to be able to resize the right edge of the last column.

One option is to set the AutoSizeMode of the last column to 'Fill' and the other columns to some other mode, like 'Not Set'. Doing this would make it so that when you resize the right edge of the second-to-last column, then the left edge of the last column will automatically fill-in that space.

Pena answered 12/2, 2019 at 19:42 Comment(0)
B
0

This is an old question but there wasn't ever a good solution/answer and probably never will be but I will add the next best thing. I came across this in my search but ended up solving it myself, so I'm adding an answer to any other poor soul that comes across this in need of help.

Unfortunately with the way the DataGridView (DGV) is it won't allow you to increase the width of the last column if it is against the width of the control itself (as in it's added horizontal scrollbars as the columns are wider than the control). If your total columns weren't wider than the control, you could widen that last column UNTIL you get to the width of the control. So how I solved it:

  1. Place the DGV inside a Panel and dock it to fill.
  2. After loading your data into the DGV, add this line of code (changing Panel1 if you rename it)

Panel1.AutoScrollMinSize = New Size(Integer.Parse(grd.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)) + 200, 0)

I used +200 but you can do what you like. What this will do is make the panel min scroll width to be wider than your total column widths allowing extra space for you to now widen the last column. It also benefits as you don't need any spacer column potentially ruining your DGV.

  1. If you want to cover all bases, then you should also add that same line of code to the grd_ColumnWidthChanged event so when you do resize a column, the panel resizes so it's always wider. You may need to consider any other event if you hide/show columns etc.
Bluenose answered 10/7, 2023 at 12:23 Comment(0)
G
0

Please Try as below

grd.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
grd.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
grd.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
grd.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
grd.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
Gaivn answered 29/9, 2023 at 10:54 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Blenny
S
-1

Why not add an extra column set to width of zero, so that the user can resize the last one you really want to display? I know it's kinda kludgy! If you care, you could probably the resizable property = false of the extra zero length cell to not allow resize. That method wasn't working properly for me. Required binding the last field to something, or else the field before it (which was my last column before adding the fake one in) would not allow resize.

For me the problem was caused by me changing the AutoSizeMode property of my last column to AllCells (which was a DataGridViewTextBoxColumn) at the suggestion of a fix to make a multicollumn textbox. But this didn't work the way I needed it to work, so I had did it a different way (by creating a dynamic textbox and putting it where I wanted it, but had left the AutoSizeMode = AllCells.

When I set AutroSizeMode = NotSet on my Last Column, then the DataGridView allows the user to resize this last column now!

Stroup answered 27/11, 2018 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.