Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,
I'm trying to automatically add new records to DataGrid. Everything works, but the DataGrid is refreshing every time. I want to keep it unchanged, only if a new record comes in, I want to add it 



C#
public class Example{
public static ObservableCollection<T> exampleObs = new ...
public void Method()
bool help = true;
do{

  exampleObs.Add(new T(...));

 }while(!help);
}


C#
public class XAMLWindow{

 private readonly object exampleLock = new object();
public XAMLWindow{
InitializeComponent();
BindingOperations.EnableCollectionSynchronization(Example.exampleObs, exampleLock);
 lock (exampleLock)
            {
                //DataGridTable.ItemsSource = Example.exampleOb;
                Example ex = null;
                for (int i = 0; i < Example.exampleOb.Count; i++)
                {
                     ex = Example.exampleOb[i];
                DataGridTable.Items.Add(ex);
                }
}

}


What I have tried:

Tried to found always the new one. Or add just items(as in example) not ItemSource property.
Posted
Comments
[no name] 27-Jul-22 12:25pm    
If you're "looping and inserting" (an unrealistic exercise) you should be doing a "begin update ... end update" action if you don't want to see "flickering" (a completely predictable result based on what you're doing).
Petr Barabáš 28-Jul-22 3:24am    
Okay, thanks for that. What do you suggest?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900