DataGrid Column AutoFill

Aug 27, 2008 at 9:36 AM

Is DataGrid support column auto fill?
For example, I have DataGrid with 3 colmuns: col0, col1 and col2.
I want to get following behavior: col0 and col2 has fixed width and col1 width is auto-resize on DataGrid width?
How can I achieve it ?
Aug 27, 2008 at 1:12 PM
Something like this should work,



    <dg:DataGridTextColumn Width="130" Header="Col0" />
    <dg:DataGridTextColumn Width="*" Header="Col1" />
    <dg:DataGridCheckBoxColumn Width="130" Header="Col2" />


Col1 Width is a star width which is proportionally sized to the DataGrid width.
Sep 12, 2008 at 7:12 AM
There seems to be a bug here. If the grid does not contain any rows, the header will not resize, it seems! Is there a workaround for this?
Sep 12, 2008 at 12:33 PM
I'm not seeing this problem.  Could you give more detail on how you are setting up your DataGrid.
Sep 12, 2008 at 2:31 PM
I found out that if i edit the xaml (instead of using the checkbox in the designer) and set it to CanUserAddRows="True" this will work, but the checkbox in the designer will always be unchecked ( I am using Blend 2.5 June 2008 Preview). But if I klick on the little white square beside "CanUserAddRows" in the designer, klick "Reset" and then check the checkbox, I will always get the above error.
Sep 12, 2008 at 2:41 PM
Sorry, I replied in the wrong thread.
For the resize of a header without any datarows I have this setup: (The datasource is just a simple list of objects, if this list is empty, the header is not resized, if there is at least one row, the resizing works as expected)








<RowDefinition Height="0.291*"/>



<RowDefinition Height="0.54*"/>



<RowDefinition Height="0.169*"/>









<ColumnDefinition Width="0.269*"/>



<ColumnDefinition Width="0.607*"/>



<ColumnDefinition Width="0.124*"/>






<Custom:DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"



IsSynchronizedWithCurrentItem="True" Grid.Row="1" Grid.Column="1" x:Name="_dataGrid"



AlternatingRowBackground="#FFC2D2D2" HorizontalGridLinesBrush="#FFD8D8D8" VerticalGridLinesBrush="#FFFFA3A3"



GridLinesVisibility="Vertical" AutoGenerateColumns="False" ItemsSource="{StaticResource Datenliste}"






Background="Transparent" BorderBrush="{x:Null}" HeadersVisibility="Column"









<Custom:DataGridTextColumn DataFieldBinding="{Binding Path=Name}" Header="Name" Width="30*" />



<Custom:DataGridTextColumn DataFieldBinding="{Binding Path=Kategorie}" Header="Kategorie" Width="100*" />



<Custom:DataGridTextColumn DataFieldBinding="{Binding Path=ID}" Header="ID" Width="30*" />











Sep 12, 2008 at 4:19 PM
hm... I copied your code and still didn't see this problem with the header resizing. 
Sep 28, 2008 at 7:45 PM

I was also trying to get "AutoFill" type functionality. I tried ColumnWidth="*", which kind of works, but I have a trigger set for IsMouseOver to change the row background and border colours and when the trigger activates the column widths seem to start oscillating, I think due to the change in the cell contents. Also, the text in the last column is obscured by the scrollbar.

I wonder if this would be improved if I only applied proportional column width to a few of the columns (I have 10 at the moment)?

Also, how can I set this in code? I have tried DataGridLength, but this doesn't have an option for "Star" or "Proportional".

Thanks for any help offered!
Sep 28, 2008 at 8:57 PM

Just fixed the "oscillating" column width. This was down to me having a different BorderThickness for IsMouseOver.

I expect the problem in the last column is down to my custom header, I will investigate!

I would still appreciate any help with how to set column width to proportional in code.

Sep 29, 2008 at 2:20 AM
The oscillating issue that you see will be fixed in the v1 release.

To set star funcitonality on a column width in code you do something along these lines (where '1.0' is an arbitrary proportion),

DataGrid_Standard.Columns[0].Width =

new DataGridLength(1.0, DataGridLengthUnitType.Star);


Oct 1, 2008 at 6:00 PM
Thanks, that works.