none
listview binding very very slow

    Question

  • Hi,

    in my WPF application I've a listView bind to a dataTable (the values come from my dataBase)

    in a table with 1200 results, the binding is very very slow (few minutes). i read hear about solution, but nothing works.

    my XAML code is:

     

    <Border x:Name="body"
                                DockPanel.Dock="Top">
                            <DockPanel LastChildFill="False">
                                <!-- Results -->
                                <ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Auto"  >
                                    <ListView x:Name="searchResultsListView" DockPanel.Dock="Top"  FontSize="12" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
                                        <ListView.Foreground >
                                            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                                <GradientStop Color="#FF000040" Offset="0" />
                                                <GradientStop Color="#FF000040" Offset="1" />
                                            </LinearGradientBrush>
                                        </ListView.Foreground>
                                        <ListView.View>
                                            <GridView x:Name="searchResultsGridView"></GridView>
                                        </ListView.View>

                                    </ListView>
                                </ScrollViewer>
                            </DockPanel>
                        </Border>

     

    my code behind is:

            private void Get_All_Records_From_DB()
            {

                SqlCommand com = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();


                //
                // com
                //

                com.CommandText = "[Get_Rec]";
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.Connection = m_mw.GetDbCon().GetConn();
                com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));

                //
                // da
                //
                da.SelectCommand = com;
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

                System.Data.SqlClient.SqlParameter name_p = com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 100));
                name_p.Value = nameSearch_txt.Text;

                System.Data.SqlClient.SqlParameter kind_p = com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@kind", System.Data.SqlDbType.Int));
                kind_p.Value = (int)m_tableType;

                m_dt.Clear();
                da.Fill(m_dt);

                dtv = new DataView(m_dt);


                GridViewColumn gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col1";
                gvcolumn.DisplayMemberBinding = new Binding("col1");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col2";
                gvcolumn.DisplayMemberBinding = new Binding("col2");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col3";
                gvcolumn.DisplayMemberBinding = new Binding("col3");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col4";
                gvcolumn.DisplayMemberBinding = new Binding("col4");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col5";
                gvcolumn.DisplayMemberBinding = new Binding("col5");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col6";
                gvcolumn.DisplayMemberBinding = new Binding("col6");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col7";
                gvcolumn.DisplayMemberBinding = new Binding("col7");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col8";
                gvcolumn.DisplayMemberBinding = new Binding("col8");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col9";
                gvcolumn.DisplayMemberBinding = new Binding("col9");
                searchResultsGridView.Columns.Add(gvcolumn);

                gvcolumn = new GridViewColumn();
                gvcolumn.Header = "col10";
                gvcolumn.DisplayMemberBinding = new Binding("col10");
                searchResultsGridView.Columns.Add(gvcolumn);

                //Set the DataContext property of the ListView to the DataTable and bind the ItemsSourceProperty to {Binding}
                Binding bind = new Binding();
                searchResultsListView.DataContext = dtv;

                searchResultsListView.SetBinding(ListView.ItemsSourceProperty, bind);

                if (m_tableType == MainWindow.TableType.PritimTableFull)
                {
                    TitleTextBlock.Text = "My Ta

    Monday, August 16, 2010 2:36 PM

Answers

  • Hi gidi,

    Could you please try to test which line consumes a lot of time? Try to set several breakpoints in this function and check it step by step, to watch which line consumes much time.

    As we know, when we bound a large collection data source to a WPF ItemsControl, the control will only create visual containers for the items that are actually visible (VirtualizingStackPanel.IsVirtualizing="True" and VirtualizingStackPanel.VirtualizationMode="Recycling"). So there is another attached property you could try: ScrollViewer.IsDeferredScrollingEnabled="True".

    If it can not help you, the following article introduces a technic - Data Virtualization may help you: http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

    Meanwhile, you could try to use the DataGrid in WPF Toolkit or WPF 4, the DataGrid has a good performance on large data.

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 17, 2010 8:32 AM
    Moderator

All replies

  • I think the problem is your database query possibly? Is that taking a long time? You could thread the call to the database, or create a few queries that each return smaller amounts so the UI can get results sooner.
    Monday, August 16, 2010 4:21 PM
  • I use the same query in my windows form application and i don't have this problem there, so I'm not sure it's related to the query.

    Monday, August 16, 2010 5:28 PM
  • If you set a breakpoint on the query, is it returning instantly? Or is the delay on your call that binds the ListView? How many items are in the collection when you bind it?
    Monday, August 16, 2010 5:39 PM
  • If you can not optimize the query then you can Asynch the binding by setting in the Binding element IsAsync="True".

    Thanks,

    Muhammad

    shujaatsiddiqi.blogspot.com


    Shujaat Siddiqi
    Monday, August 16, 2010 5:44 PM
  • i read here about this problem, and i saw that many people have it.

    i read about virtualization but i don't understand how to use it

    Monday, August 16, 2010 5:52 PM
  • You have to use VirtualizingStackPanel for this.

    http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.aspx

    Thanks,

    Muhammad

    shujaatsiddiqi.blogspot.com


    Shujaat Siddiqi
    Monday, August 16, 2010 6:00 PM
  • Hi gidi,

    Could you please try to test which line consumes a lot of time? Try to set several breakpoints in this function and check it step by step, to watch which line consumes much time.

    As we know, when we bound a large collection data source to a WPF ItemsControl, the control will only create visual containers for the items that are actually visible (VirtualizingStackPanel.IsVirtualizing="True" and VirtualizingStackPanel.VirtualizationMode="Recycling"). So there is another attached property you could try: ScrollViewer.IsDeferredScrollingEnabled="True".

    If it can not help you, the following article introduces a technic - Data Virtualization may help you: http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

    Meanwhile, you could try to use the DataGrid in WPF Toolkit or WPF 4, the DataGrid has a good performance on large data.

    Sincerely,

    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 17, 2010 8:32 AM
    Moderator
  • thanks, i moved to dataGrid...
    Wednesday, August 18, 2010 4:14 PM