How do I make XAML DataGridColumns fill the entire DataGrid?

125,632

Solution 1

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.

Solution 2

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.

Solution 3

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}" />

Solution 4

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

Solution 5

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

Share:
125,632
Purplegoldfish
Author by

Purplegoldfish

Currently I work as a Software Engineer in England. I'm developing software used in the food labeling industry. My work is mainly done using C#, SQL and WPF / XAML.

Updated on July 08, 2022

Comments

  • Purplegoldfish
    Purplegoldfish almost 2 years

    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?

  • Oleg Vazhnev
    Oleg Vazhnev over 12 years
    and how to do similar thing if i'm using AutoGenerateColumns="True"?
  • ChrisF
    ChrisF over 12 years
    @javapowered - I'd suggest asking your own question, referencing this one.
  • Xavier
    Xavier about 12 years
    @javapowered <DataGrid AutoGenerateColumns="True" ColumnWidth="*" ItemsSource={Binding} />
  • quetzalcoatl
    quetzalcoatl almost 12 years
    note: if for some reason, after setting ColumnWidth to "*" you dont see any effect, change the ItemsSource for a second and then undo it - the designer has to be kicked to reload the data, or else the columns may stay stale and dont notice the new style.
  • G K
    G K about 9 years
    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 pastie.org/10085815
  • Steve
    Steve almost 9 years
    @MohamedSakherSawan it does indeed work for datagrid. Both ColumnWidth="*" on the DataGrid and Width="*" on the indivdual columns have the desired effect
  • 0x12
    0x12 over 8 years
    Gives an Error Sting cannot be converted to '*'
  • Bryan Harrington
    Bryan Harrington over 5 years
    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.
  • Rudolf Dvoracek
    Rudolf Dvoracek over 4 years
    @Steve it works only if you set width on DataGrid itself.
  • Rock Junior
    Rock Junior almost 4 years
    @co2f2e did u do it inside DataGridTextColumn (Sorry it late but i see nobody replied your Error) ==> <DataGrid > <DataGrid.Columns> <DataGridTextColumn Header="ColumnTitle1"/> <DataGridTextColumn Header="Name" Width="0.25*"/> <DataGridTextColumn Header="ColumnTitle3" Width="0.25*"/> </DataGrid.Columns> </DataGrid>
  • Stan1k
    Stan1k over 3 years
    In my case the accepted answer also wasn't enough. After adding HorizontalAlignment="Center" like you mentioned, it fixed the issue. Thanks!