141

I am using DataGrids in XAML (not Silverlight) with resizable columns, the DataGrid will expand if the user resizes the screen.

Currently if the widths of all the DataGrid columns are less than the width of the DataGrid I get an extra "column" appearing which is unclickable and serves no purpose.

Does anyone know how to make one column always resize to fill all the remaining space?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59

11 Answers11

288

If you use Width="*" the column will fill to expand the available space.

If you want all columns to divide the grid equally apply this to all columns. If you just want one to fill the remaining space just apply it to that column with the rest being "Auto" or a specific width.

You can also use Width="0.25*" (for example) if you want the column to take up 1/4 of the available width.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
21

Make sure your DataGrid has Width set to something like {Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window,AncestorLevel=1}}.

Like that, your setting of Width="*" attribute on DataGrid.Columns/DataGridXXXXColumn elements should work.

Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42
MStack
  • 328
  • 3
  • 6
15

As noted, the ColumnWidth="*" worked perfectly well for a DataGrid in XAML.

I used it in this context:

<DataGrid ColumnWidth="*" ItemsSource="{Binding AllFolders, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
8

My 2 Cent ->

Very late to party

DataGrid -> Column -> Width="*" only work if DataGrid parent container has fix width.

example : i put the DataGrid in Grid -> Column whose width="Auto" then Width="*" in DataGrid does not work but if you set Grid -> Column Width="450" mean fixed then it work fine

7

Set the columns Width property to be a proportional width such as *

devdigital
  • 34,151
  • 9
  • 98
  • 120
3

Another spin on the same theme:

protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
{
    dataGrid.Width = e.NewSize.Width - (e.NewSize.Width * .1);

    foreach (var column in dataGrid.Columns)
    {
       column.Width = dataGrid.Width / dataGrid.Columns.Count;
    }
 }
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Steve
  • 549
  • 6
  • 21
2

I added a HorizontalAlignment="Center" (The default is "Strech") and it solved my problem because it made the datagrid only as wide as needed. (Removed the datagrid's Width setting if you have one.)

enter image description here

JBrooks
  • 9,901
  • 2
  • 28
  • 32
  • 1
    Combined with the selected answer and this one it solved the problem for me. I needed to remove the width on the Datagrid itself. Thanks. – Bryan Harrington Feb 26 '19 at 16:25
  • In my case the accepted answer also wasn't enough. After adding `HorizontalAlignment="Center"` like you mentioned, it fixed the issue. Thanks! – Stan1k Jan 22 '21 at 18:26
1

This will not expand the last column of the xaml grid to take the remaining space if AutoGeneratedColumns="True".

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
pramod
  • 11
  • 2
  • I have removed the AutoGenerateColumns, even though the columns are not divided or stretched the entire width of the data grid/screen. I have the corresponding row of the grid to be "*" and the columns width does not have any width specified (either "auto" or "some value"). But still I am having the issues, here is the xaml code of my design http://pastie.org/10085815 – G K Apr 11 '15 at 04:38
0

set ONE column's width to any value, i.e. width="*"

alkk
  • 331
  • 5
  • 11
0

For those looking for a C# workaround:

If you need for some reason to have the "AutoGeneratedColumns" enabled, one thing you can do is to specify all the columns's width except the ones you want to be auto resized (it will not take the remaining space, but it will resize to the cell's content).

Example (dgShopppingCart is my DataGrid):

dgShoppingCart.Columns[0].Visibility = Visibility.Hidden; 
dgShoppingCart.Columns[1].Header = "Qty";
dgShoppingCart.Columns[1].Width = 100;
dgShoppingCart.Columns[2].Header = "Product Name"; /*This will be resized to cell content*/
dgShoppingCart.Columns[3].Header = "Price";
dgShoppingCart.Columns[3].Width = 100;
dgShoppingCart.Columns[4].Visibility = Visibility.Hidden; 

For me it works as a workaround because I needed to have the DataGrid resized when the user maximize the Window.

Exel Gamboa
  • 936
  • 1
  • 14
  • 25
0

It worked well for me, just put columnwidth dependency property as columnwidth="*" it will fill columns widths to datagrid width like as winforms(autosize=fill)

 <DataGrid Grid.Row="0" x:Name="dg1" VerticalAlignment="Top" AutoGenerateColumns="False" Margin="0,0,-6,0" Width="1520" Height="700"  CanUserAddRows="True" 
        
        CanUserDeleteRows="True" ItemsSource="{Binding EmployeeData}" RowDetailsVisibilityMode="Visible" HorizontalGridLinesBrush="DarkBlue" VerticalGridLinesBrush="DarkGreen" ColumnWidth="*" >
Vintage Coder
  • 441
  • 1
  • 6
  • 9