3
votes

i have 2 datagrid that i have problem with. the height goes completly nuts as soon as data is filled into them.

i got the following sample format :

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!-- some control on row 0 -->

    <Grid Grid.Row="1" HorizontalAlignment="Stretch"  Margin="0,2,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.66*"/>
            <RowDefinition Height="0.33*" />
        </Grid.RowDefinitions>

        <DataGrid Name="grdSeacrhData" Grid.Row="0" />

        <DataGrid Name="grdSelectData" Grid.Row="1" />
    </Grid>
</Grid>

so the main grid rows size is because first row have bunch of control with unknown height yet and is the most important part.

Second row goal is to show 2 datagrid first slightly bigger to show result from above and second one smaller for refined result of one or many selection from the first one.

if i do not touch anything and open the form it works beautifully. but as soon as the first datagrid "grdSeacrhData" gets data displayed inside in the order of 200-300 records the height is stretched outside the window size.

i tried vertical alignment and vertical content alignment, setting vertical scroll to visible, auto and also tried putting into to stack panel, dock panel, canvas, scrollviewer ...

I am out of idea. all i know is if i set the height or max height of the datagrid then it works. is there a way to prevent datagrid items to stretch the datagrid height if not set ?

Edit #1 :

Ok i have tried something else with WPF just to see result and it also fail to work. here some detail :

I made 1 grid with 3 rows first row height or 100 second row height of auto third row height of 100

i've put a datagrid in the middle row and it works perfectly. now as soon as i add data in it goes off screen.

without data in the datagrid i stretch the window and it's perfect the top row stay at 100 the bottom too and the middle just adjust itself.

Now here what i've tried in Winforms

Put a datagrid anchored top botton left and right. top value = 100 and bottom value = form.height - 100

now strech the form it works perfectly. i added same xml data and the format stayed

So what i am tying to do is not just some "magical unicorn idea's" it's actually possible very easily in winforms apparently but i do NOT want to use winforms i want to stay with WPF. how then can replicate that ?

for the kick i just tested ASP.Net datagrid and it behave like winform test so definitively it's possible.

2

2 Answers

0
votes

You can use the MaxHeight. It will set the maximum height that tha element could have.

<DataGrid Name="grdSelectData" Grid.Row="1" MaxHeight="400" />

So, if you have less data it will be smaller. At the time that you add more data, it will resize until reach the MaxHeight.

.

0
votes

Replaced everything with winform user control as it have better layout settings. Had tried with browser object and HTML but some issue with filling data so went for winforms in the end