user350213
user350213

Reputation: 385

Trying to load a very long list to WPF DataGrid

I have a very large xml file, 4,677 KB - basically around 26000 lines, each looking like:

<samples>
<sample id=1 field =test field1=test1 field2=test2 field3=""test3"" field4=""test4"" field5=""test5"" field6=""test6"" field7=""test7""  field8=""test8"" field9=""test9"" field10=""test10""   />
<sample id=""2"" field =""test"" field1=""test1"" field2=""test2"" field3=""test3"" field4=""test4"" field5=""test5"" field6=""test6"" field7=""test7""  field8=""test8"" field9=""test9"" field10=""test10""   />
<sample id=""3"" field =""test"" field1=""test1"" field2=""test2"" field3=""test3"" field4=""test4"" field5=""test5"" field6=""test6"" field7=""test7""  field8=""test8"" field9=""test9"" field10=""test10""   />

...

and I'm trying to load this into a WPF DataGrid using a DataSet/DataView. Sure enough, OutOfMemoryException was fired when trying to bind to this as so:

        <Grid x:Name="LayoutRoot">
        <HeaderedContentControl Header="Sample Data">
        <DataGrid
                x:Name="DataGrid"
                AutoGenerateColumns="True"
                ItemsSource="{Binding Path=GridData, Mode=OneWay}">
            </DataGrid>
        </HeaderedContentControl>
    </Grid>

I tried adding the VirtualizingStackPanel.IsVirtualizing="True" (although that's the default) and also VirtualizingStackPanel.VirtualizationMode="Recycling", but it didn't help.

I know this is tons of data for the control, I'm wondering what my options are.

Upvotes: 0

Views: 1013

Answers (3)

Steven S.
Steven S.

Reputation: 734

Try setting the Width and Height of the DataGrid to fix values. This is what fixed the problem in our case.

<Grid x:Name="LayoutRoot">
    <HeaderedContentControl Header="Sample Data">
        <DataGrid
            x:Name="DataGrid"
            Width="600" Height="400"
            AutoGenerateColumns="True"
            ItemsSource="{Binding Path=GridData, Mode=OneWay}">
        </DataGrid>
    </HeaderedContentControl>
</Grid>

Upvotes: 1

Gus
Gus

Reputation: 10659

I agree with HCL that this does not seem to be related to the rendering of these controls. My suggestion would be for you to have a view model with a separate data structure bound to the View. Then load the data structure from your xml file. This way you separate the tasks and you'll narrow down to the problem. Certainly the binding piece is not the culprit.

Upvotes: 0

HCL
HCL

Reputation: 36795

I don't think that 4,7Megs of data will give you an out of memory exception. Furthermore, as you already wrote, DataGrid works in virtualizing mode. Do you get the exception after setting the ItensSource?
Try loading the data without attaching it to the DataGrid. Then you know if the problem is the loading code. I would check the loading code for an infinite loop.

Upvotes: 0

Related Questions