Home > Net >  WPF DataGrid ItemsSource memory leak
WPF DataGrid ItemsSource memory leak

Time:12-20

I don't know why but when I define the ItemsSource of my DataGrid, it creates some memory leak.

This is the xaml code of my DataGrid :

<DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" Margin="20,250,20,20" SelectionUnit="Cell" Style="{DynamicResource MaterialDesignDataGrid}" SelectionMode="Single"/>

And this is the code behind where I define the ItemsSource :

cmd = New SqlCommand With {
    .CommandText = strsql,
    .Connection = DBConn.ADONETconn
    }
da = New SqlDataAdapter(cmd)
dt = New DataTable("RECH")
da.Fill(dt)
DataGrid1.ItemsSource = dt.DefaultView

As you can see from the diagnostic tool, I have some memory leak somewhere. enter image description here

I know it's caused by the line DataGrid1.ItemsSource = dt.DefaultView because if I comment it, the memory leak doesn't occur.

I've downloaded JetBrains dotMemory to see what's going on. As you can see bellow, it tells me that it's caused by DataGridRow. enter image description here

I don’t understand why this happens. The data is correct in the DataTable. enter image description here

If you have any clues, please share them with me

CodePudding user response:

Found ! Actually it came from the fact that my DataGrid was in a StackPanel. And I still don’t know why but it was a problem.

  • Related