how to get the wpf toolkit datagrid to show new rows when bound to dataset

谁都会走 提交于 2019-12-06 06:58:05

You can set the datagrid.ItemsSource to an ObservableCollection<T>.

ObservableCollection<YourItem> items = new ObservableCollection<YourItem>();
yourDataGrid.ItemsSource = items;

Then you should be able to just add to the collection to get the new rows to appear:

Edit: Based on updated info.

if (Dispatcher.CheckAcces())
{
    // already on thread UI control was created on
    items.Add(<your item>);
}
else
{
    // update on same thread UI control was created on
    // BeginInvoke would invoke a delegate which would call items.Add(<your item>)
    Dispatcher.BeginInvoke(...);
}

See Dispatcher. All System.Windows.UserControl objects have a Dispatcher property.

I'm not sure how anyone would have figured this out, especially since I didn't mention it, but the problem was that I was updating the dataset from a thread other than the form's main thread, i.e. the thread that the grid was created on. You can do updates from another thread, but you can't do inserts, although I don't know why. If someone could shed some light on that, I'd appreciate it. My guess would be that the grid checks to see if the insert is coming in on another thread, and if so, it ignores it because that would cause an exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!