locked
DataTable placed in a DataGrid freezes when scroll reaches bottom RRS feed

  • Question

  • I need to generate a table in a wpf window from a tex file. Workflow:

    Click button --> pick, open and read file --> parse file --> create table --> update data grid with table view 

    Code:

              
            private void OpenTexButton_Click(object sender, RoutedEventArgs e)
            {            
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                // Set filter for file extension and default file extension 
                dlg.DefaultExt = ".tex";
                dlg.Filter = "TEX Files (*.tex)|*.tex|Text Files (*.txt)|*.txt";
    
                Nullable<bool> result = dlg.ShowDialog();
    
                // Get the selected file name and display in a TextBox 
                if (result == true)
                {
                    // Open document 
                    string fileName = dlg.FileName;
                    RenderTexAsync(fileName);
                }
            }
    
            private async void RenderTexAsync(string fileName) 
            {
                byte[] result; 
    
                using (FileStream SourceStream = File.Open(fileName, FileMode.Open))
                {
                    result = new byte[SourceStream.Length];
                    await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
                }
    
                string content = System.Text.Encoding.ASCII.GetString(result);
                ParseTable(content);
            }
    
            private void ParseTable(string content)
            {
                DataTable dataTable = InitializeTable(table);
                // Set binding
                appliedJobs.ItemsSource = dataTable.AsDataView();      
            }
    
    

    xml:

    <DataGrid Name="appliedJobs" Background="#50000000" Grid.Row="1" Grid.Column="1" Margin="10,10,10,10" ItemsSource="{Binding dataTable, IsAsync=True}" AutoGenerateColumns="False" />

    Setting IsAsync to true produces a table with empty rows; setting it to false produces the correct table, but upon scrolling 

    down to the end, the UI freezes. The UI should be updated asynchronously with the newly created table. 

    How can I make it async?

    Thursday, October 1, 2020 11:25 AM

All replies

  • Hello,

    The first thing to do is get rid of async void and use async Task, see the following article. What happens?

    Also What is InitializeTable doing?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 12:12 PM
  • I'll use Task instead but the problem is that the UI is not updated properly (in debug mode the table is created as expected). InitializeTable() parses the contents of a string and creates a DataTable. This works as intended but the UI is not updated as expected. If "IsAsync" is removed from the attributes of the DataGrid then the table is rendered onto the DataGrid but freezes when scrolling to the bottom end of the table. 
    Thursday, October 1, 2020 12:31 PM
  • Did you try Task yet instead of void?

    Is InitilizeTable async?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 12:42 PM
  • Did you try Task yet instead of void? 

    Not yet. The problem seems related to binding (UI not updated with what's (correctly) created in the background).

    Is InitilizeTable async?

    No. It reads a text file and parses it into a DataTable whose view is attached to the DataGrid. 


    Thursday, October 1, 2020 1:23 PM
  • Hi Vadim Ivanof,
    Based on your description, it is mostly related to WPF. So it is recommended to ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 2, 2020 2:32 AM