Height of WPF DataGrid increases when data added
Asked Answered
R

7

8

I have a WPF DataGrid that increases in height when I add data to it that won't fit inside its initial height. I don't want the height to change unless the user increases the Window size. Is there a way to stop this auto-resize?

<Window x:Class="WpfDataGridSizeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Loaded="Window_Loaded"
        SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Top" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Grid>
</Window>

    public partial class MainWindow : Window
    {
        MyList myList = new MyList();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            wordsDataGrid.DataContext = myList;
            for (int i = 0; i < 100; ++i)
            {
                myList.AddOne("blah blah");
            }
        }
    }

    public class MyList : ObservableCollection<AClass>
    {
        public MyList() { }

        public void AddOne(string aString)
        {
            base.Add(new AClass(aString));
        }
    }

    public class AClass
    {
        public string AString { get; set; }

        public AClass(string aString)
        {
            AString = aString;
        }
    }
Rhinitis answered 12/6, 2012 at 10:16 Comment(2)
Put it in a RowDefinition that has Height="*"Newsreel
I tried this @Meleak but it it makes no difference.Rhinitis
B
5

If I don't get you wrong, you want your DataGrid to be in centain Height initally, with some Empty Space below the DataGrid within the Windows...Then when resize the Window, the DataGrid will changes it size.

Add one more Row to the Grid, and define a MinHeight of that Row. Then set DataGrid to be VerticalAlignment = Stretch. Also set a default height size for the Window.

<Window x:Class="WpfDataGridSizeTest.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Loaded="Window_Loaded" Height="300"> 
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="*"/> 
            <RowDefinition MinHeight="100"/> 
        </Grid.RowDefinitions> 
        <DockPanel Grid.Row="0" VerticalAlignment="Stretch"> 
            <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Stretch" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" > 
                <DataGrid.Columns> 
                    <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/> 
                </DataGrid.Columns> 
            </DataGrid> 
        </DockPanel> 
    </Grid> 
</Window> 
Brahmaputra answered 12/6, 2012 at 13:53 Comment(4)
Not quite. I want the height of the DataGrid to be independent of how many rows it contains, to remain the same height it is in the design view. The user may change the size of the Window which may, depending on RowDefinitions, change the size of the Grid row. In that case I want the height of the GridView to grow with the Grid height.Rhinitis
The above code is independent of how many rows it contains, the DataGrid will stays the same height until user resize the the Window. The Row one (The one that contains DataGrid) is using remaining Height that left within the Window. Say now Window is 300, used 100 in 2nd Row, 200 will use for DataGrid. When user resize the Window to 500, 400 will be use for DataGrid...No matter how many row the DataGrid contains, it won't increase it height.Brahmaputra
Sorry, I seem to have made an error when I first tested your solution. This does seem to work.Rhinitis
If you don't want the 2nd Row (those empty space), you can just remove the 2nd RowDefinition. The important here is to let both of your DockPanel and DataGrid to Stretch to height of the Window. And to have your Window have a default Height. If this is the answer you want, please mark it as answer :)Brahmaputra
A
5

I had the same problem, and I fixed it by simply removing SizeToContent="WidthAndHeight" in the Window definition.

Ard answered 8/1, 2016 at 0:35 Comment(1)
I did what the accepted answer suggested but didn't work without this -obvious but easy to missInfinity
P
1

This will do what you desire:

<Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="1*"/>
      <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="70"/>
      <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <DataGrid Name="dgMain" AutoGenerateColumns="True"></DataGrid>

    </Grid>

alongside with this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Foo foo;
        List<Foo> foos = new List<Foo>();

        foo = new Foo() { Name = "Sjaak" };
        foos.Add(foo);

        foo = new Foo() { Name = "Joepie" };
        foos.Add(foo);

        dgMain.ItemsSource = foos;

    }
}

public class Foo
{
    public Foo() { }

    public String Name { get; set; }
}

The trick is in the proportional (even) distribution of the row heights by using "1*" for the Height properties of both rows. You can also ditribute it in other "shares" by setting one row Height to "2*" etc.

Petrel answered 13/6, 2012 at 8:6 Comment(0)
P
0

Try using VerticalAlignment = Top for instance or explicitly set the Height property to some value.

This might also help to put the DataGrid in a StackPanel and controlling the sizing behavior of the StackPanel.

Petrel answered 12/6, 2012 at 10:18 Comment(1)
Setting VerticalAlignment="Top" doesn't help. I don't want to set Height explicitly because I do want the grid to grow when the Window size is increased by the user. I just want the height to be as it is before the data is added. I'm not sure what properties to set on an enclosing StackPanel to achieve what I want.Rhinitis
C
0

If the problem is the entire grid growing (but you're happy for individual rows to grow) then: what's the parent panel that the DataGrid is inside? Changing this can affect the way the control resized.

For example: a StackPanel will allow its children to grow in one direction (the direction of stacking), and a DockPanel will limit the size of the "filled" element to the available space.

Campanulaceous answered 12/6, 2012 at 10:45 Comment(1)
The row size is not changing. I'm just displaying text. I've tried having the DataGrid being the child of the Window, of a Grid and a DockPanel within a Grid. In all cases the DataGrid grows to a little more than the hight of my screen when I add rows.Rhinitis
M
0
 VerticalAlignment="Bottom" 

worked for me to resolve this exact same problem. I also used

 VerticalScrollBarVisibility="Auto"     
Maurita answered 8/11, 2017 at 14:3 Comment(0)
R
0

This answer maybe came late but hope it helps someone what about creating a little method "ResizeDataGrid":

    Private void ResizeDataGrid (DataGrid myTable,double currentWindowSize){ 
// How much size would your datagrid occupy of total size , 1: full window
    var amountTakenofFullSize = 0.75;
    //Change the amount taken so it fits to ur content 
    If(windowSize>800)
     amountTakenofFullSize=0.85;


    mainTable.Height=currentWindowSize * amountTakenofFullSize;
    }
        
        
        

Then you can call it on window loaded or size changed Call the method by using the paramètres :

ResizeTable(myDataGrid,(neededWindow).RenderSize.Height);
Reconvert answered 21/5, 2022 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.