Click here to Skip to main content
15,942,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
       In my WPFapplication I have data grid... which can collapse and expand... But my problem is now when the Data grid is expanding the height of data grid is being increased... But when clicking collapse after expanding the records are collapsed , but the height of data grid is being constant. Can any one please advice me that how to set the height of data grid dynamically in WPF c#
Updated 31-Aug-17 3:48am

1 solution

You can set you
HorizontalAlignment=Stretch and

If this doesn't work,Bind the Grid's Height to the Window Height so it doesn't auto-grow to fit it's contents.

Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}" (It might be RenderSize.ActualHeight insetad of just ActualHeight)

Another alternate is to use DockPanel instead of a Grid since that control doesn't auto-grow to fit it's contents. Instead it'll stretch it's last child to fill all remaining space.
Share this answer
jing567 29-Nov-13 4:23am    
No it does not effect my problem... Is there any way to set the height of the data grid to auto dynamically in code side? in wpf c#
AnthonyMG 29-Nov-13 5:26am    
Try to get do below stuff in code behind.

for(int i = 0 ; i < dataGrid.Columns.Count; i ++)
dataGrid.Columns[i].Height = new DataGridLength(0, DataGridLengthUnitType.Auto);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900