none
DataGird的ItemsSource绑定一个DataTable的DefaultView,滚动后,列表没数据了,请问怎解解决?(急需帮助) RRS feed

  • 问题

  • DataGird的ItemsSource绑定一个DataTable的DefaultView,开启一个线程导出Excel后,滚动列表,数据全都没有了,假如绑定一个ObservableCollection集合后,同样操作就有数据。

    2017年8月9日 7:06

答案

  • Hi,

    据我所知,ObservableCollection实现了属性通知,所以当ObservableCollection的数据添加,删除,刷新时,会通知UI更新。而DataTable没有实现属性通知,根据你的描述, 我建议你首先调试你的程序,看看滚动列表的时候到底发生了什么。你也可以贴上可以复现问题的代码或者上传你的code demo, 这将帮助我们快速的分析你的问题,我们会下载并调试你的程序。另外,我建议你避免使用DataTable,你可以尝试将DataTable转成ObservableCollection。


    public class GenericObject
    {
    
        private readonly ObservableCollection<GenericProperty> properties = new ObservableCollection<GenericProperty>();
    
        public GenericObject(params GenericProperty[] properties)
        {
            foreach (var property in properties)
                Properties.Add(property);
        }
    
        public ObservableCollection<GenericProperty> Properties
        {
            get { return properties; }
        }
    
    }
    
    public class GenericProperty : INotifyPropertyChanged
    {
        public GenericProperty(string name, object value)
        {
            Name = name;
            Value = value;
        }
    
        public string Name { get; private set; }
        public object Value { get; set; }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
    private static ObservableCollection<GenericObject> Convert(DataTable toConvert)
    {
        ObservableCollection<GenericObject> _result = new ObservableCollection<GenericObject>();
    
        foreach (DataRow _row in toConvert.Rows)
        {
            GenericObject _genericObject = new GenericObject();
            foreach (DataColumn _column in toConvert.Columns)
            {
                _genericObject.Properties.Add(new GenericProperty(_column.ColumnName,_row[_column]));
            }
            _result.Add(_genericObject);
        }
    
        return _result;
    }


    Best Regards,

    Bob



    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.



    2017年8月9日 11:16
    版主