0

I have been trying to update the DataGrid instantly but the DataGrid is only updating after completing the Button_Click function that I called, Here is a sample of my code, rows variable is binded to DataGrid

public ListCollectionView rows;
private void Button_Click(object sender, RoutedEventArgs e)
{
     for(int i=0;i<5;i++)
     {
        function1();
        //Adding values to rows variable
        //Update the DataGrid at this instant with current data available
        //DataGrid.Refresh();
        System.Threading.Thread.Sleep(5000);
     }
}
SVT
  • 1
  • 2
  • You must not call Thread.Sleep in the UI thread, because it blocks that thread. – Clemens Jun 23 '22 at 18:06
  • @Clemens I have added thread sleep to describe that every iteration takes some time, actually function1() takes long time( about 30s) to complete, that's why I need to update instantly after function1(), so that I do not have to wait for next iteration results, Can you give me a method to update the datagrid – SVT Jun 24 '22 at 15:11
  • The method could be async. Or you wrap the call in `await Task.Run(() => function());` – Clemens Jun 24 '22 at 16:11
  • ok here is some code, could you please suggest based on this, I need to display person names in grid one by one at a time `ObservableCollection items; private void Button_Click(object sender, RoutedEventArgs e) { for(int i=0;i<5;i++) { items.add(new Person() { Id = i+1, name = $"namei" }); } } public class Person{ public int Id{get; set;} public string Name{get; set;} }` – SVT Jun 24 '22 at 17:57
  • @Clemens is there any datgrid.refresh or update function – SVT Jun 25 '22 at 07:00

0 Answers0