Change DataGrid width on resizing its columns - wpf

I have a DataGrid with a custom theme. Right now, if I resize a column and make it wider, it tries to squeeze other columns to make space for this column.
Instead, is it possible to increase the width of the entire DataGrid with the increase in width of a column. It's OK if it shows a horizontal scrollbar at the bottom.
EDIT: Just to clarify, each column has a MinWidth set to it. So if the overall width > window width, I see a scrollbar. But in cases where I have a datagrid <= window width, and I expand one of its column, the width of the datagrid should theoritically increase at the point when the other columns have shrunk to their MinWidths. At that point, instead of letting the DataGrid grow in width, it doesn't let me expand the column any further. Instead, I want the DataGrid to expand and accommodate the increased space.

It sounds like one of your columns has a Width="*", which means that column is assigned to all remaining space and grows/shrinks as needed instead of letting the DataGrid grow/shrink as requested and showing it's HorizontalScrollBar.

The datagrid's default behaviour is the one you need! If your Datagrid is not showing a horizontal scrollbar maybe because it's contained in a control that's constraining its width. Try to put it inside a Grid, and be sure that you haven't set the 'Width' property of any control (including the Window) that is containing it.

that's what column's MinWidth is designed for. Just set it for the columns, which you don't want to shrink. to limit the max width of the grid use the ScrollViewer:)

Related

WPF DataGrid match all columns total width

My DataGrid is bound to a collection which can not change in run-time.
I wanted to ensure that when resizing the DataGrid columns the total width of the columns won't be
longer than the DataGrid width, so that no column will disappear and no horizontal scroll will appear, never find an answer to that.
So I wanted to no if there is a way that i can bind my DataGrid width to the total width of his columns so i can make the columns unresizeable and still have the columns width to "Auto" so no data will be missing while the columns is not resizeable?
Or the first option which i didn't find - limit the resize of the columns so their total width wont be longer?
Thank's a lot and please no Code-Behind soluton.

WPF Grid Row Height Auto

This seems close to some other questions I've seen out here, but not close enough...
I have a grid with three rows. The first row holds a custom grid that I want to have as much space as possible. I set its Height to "*". The second row is a row of buttons; it is "Auto". The third row is a user control that contains another custom grid that has an unpredictable number of rows (none at design-time, filled in at run-time).
If I set the third row to "Auto", it doesn't get any space at all. I need to understand what I have to do after I load the third-row's grid to make it then figure out what "Auto" should be. Or, if "Auto" doesn't apply, how else do I get the third row to take up Just Enough space?
Does my custom grid have to fire something like a resized event after it is loaded?
You shouldn't have to do anything
If the 3rd row's Height is Auto, it will resize according to the size of it's contents. If the size of the contents change at runtime, so will the size of the Row.
You can also set limitations on the Row's Height by setting MinHeight and MaxHeight on the 3rd Row so it doesn't get smaller or larger than a specified height.
Cells will collapse to fit their contents. If the contents don't have any specified size then the cell will collapse to nothing. I'm guessing for your middle row the buttons have fixed sizes, so the row collapses to the size of the buttons, but your third row has nothing with a fixed size so it collapses to nothing. Try giving your third row a fixed size either for the whole row or for each row in the contained grid.

WinForms autosize vertically while leaving width adjustable?

Is it possible to use WinForms layout engine to set up a form in such a way that the height is adjusted completely automatically to fit the content, but the width would be adjustable by the user?
As an example, consider the following somewhat contrived form:
TLP: TableLayoutPanel, with 1 row and 2 columns.
label1: Label, contained in the left column. Set Text to something long, AutoSize to false, Dock to Fill.
button1: Button, contained in the right column. Set AutoSize to true.
Now set the row to "AutoSize", the first column to "100%" and the second column to "AutoSize".
Finally, set the whole TLP to AutoSize true, AutoSizeMode to GrowAndShrink.
At this point, the TLP could reasonably be resized horizontally, but WinForms doesn't seem to allow this.
The idea is that the form containing this TLP would grow or shrink vertically based on the width, which the user can change by resizing the window as usual.
Hopefully you have got the solution by now. But in case anybody having a same query, You can set width of Maximum Size Property to the Actual Width of the Form and the Height of MaximumSize to 0.
This is entirely possible. To do this, you have to override SetBoundsCore on your form. In this you can then control the height such that it doesn't change when a resize might want it to. Just store the height you want it to be in a variable and then have your override always set it to that. This way, when you auto-size the height, you save the new height aware before setting form Height and SetBoundsCore will honour that new height.

Horizontal ScrollBar in WinForms DataGridView

I'm using a DataGridView in VB.Net in VS 2005. The issue is, when there are rows that exceed the width of the DataGridView, the horizontal scroll bar doesn't appear (the ScrollBars property is set to "Both"), but an ellipsis appears at the end of each row that is too long. I don't want the ellipsis, but the horizontal scroll bar. The vertical scroll bar works as expected when the number of rows exceed the height of the DataGridView. Any ideas on how to resolve this?
I have tried setting numerous related properties (changing the AutoSizing property of the DataGridView, Resizability of the Row Template, made sure that no Columns were Frozen) to no avail.
Thanks!
Set the AutoSizeMode of the problematic column to AllCells.
This will resize the column header and force it to go out of the visible area thus displaying the horizotal scrollbar.
I just had the same issue. I had set the last column to Fill, so it was always resizing to fit to the visible area.

MaxAutoWidth in WPF

Is there a good way to limit the Auto Width of an element or Grid Column?
I want to limit the Auto Width only, but allow any user changes.
For example, I want to allow GridSplitter to arbitrarily resize the column, but the initial (Auto) Width should not exceed a specific value. Same for the elements -- I want to allow resizing, but limit the Width they have if never resized.
MaxWidth works exactly as opposite -- it does not affect Auto Width of Grid Columns (it might be a bug), but limits the resized width.
Is there any good solution that works for (almost) any element or Grid part?
Try binding the MaxWidth to the Width and use a IValueConverter to react to changes from Auto to specific values.
In the grid column set the width you want the column to start at. Any changes to the column width made by the user using the grid splitter will overwrite the width you start with.

Resources