Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ObservableCollection没有向datagrid添加新行吗?_C#_Wpf_Observablecollection - Fatal编程技术网

C# ObservableCollection没有向datagrid添加新行吗?

C# ObservableCollection没有向datagrid添加新行吗?,c#,wpf,observablecollection,C#,Wpf,Observablecollection,这是我的第一个WPF应用程序,我需要一些帮助 我创建了一个WPF窗口,其中包含使用EF、CollectionViewSource绑定到数据库的Datagrid 从窗口上的“数据源”窗口拖放表时,MDI会自动生成代码 ObjectQuery<ITPORTAL.HRMS_IT_ASSET_MASTER> ass = gRACHRMSEntities.HRMS_IT_ASSET_MASTER; 但是这段代码不支持过滤功能,这就是为什么我添加了这样的东西 private Observab

这是我的第一个WPF应用程序,我需要一些帮助 我创建了一个WPF窗口,其中包含使用EF、CollectionViewSource绑定到数据库的Datagrid 从窗口上的“数据源”窗口拖放表时,MDI会自动生成代码

ObjectQuery<ITPORTAL.HRMS_IT_ASSET_MASTER> ass = gRACHRMSEntities.HRMS_IT_ASSET_MASTER;
但是这段代码不支持过滤功能,这就是为什么我添加了这样的东西

 private ObservableCollection<ITPORTAL.HRMS_IT_ASSET_MASTER> Load_Asset_Master_Data(System.Data.Objects.ObjectQuery<ITPORTAL.HRMS_IT_ASSET_MASTER> objectSet)
        {
            ObservableCollection<ITPORTAL.HRMS_IT_ASSET_MASTER> oc = null;
            try
            {
                var value = from Asset_Master in objectSet
                            where Asset_Master.IT_ASSET_IS_ACTIVE == "1" && Asset_Master.IT_ASSET_IS_SCRAPED == "0" && Asset_Master.IT_ASSET_IS_SCRAP_FINAL == "0"
                            select Asset_Master;
                oc = new ObservableCollection<HRMS_IT_ASSET_MASTER>(value.ToList());
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "IT Portal Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
            return oc;
        }
这样我就可以做过滤了 但我失去了一个功能,因为 当我填充datagrid末尾的行并按save时 保存命令不考虑添加的行, save命令仅响应编辑操作 下面是保存事件处理程序

private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {                                               
                if (gRACHRMSEntities.SaveChanges() > 0)
                {
                    txtblkStatus.Text = "Changes has been saved.";                
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);

        }       }
我在网上查过了,我发现
使用ObservableCollection时,我必须为T提供自己的IEditableObject实现。

我认为您应该编写

hRMS_IT_ASSET_MASTERViewSource.ItemsSource =Load_Asset_Mast ...
而不是:

hRMS_IT_ASSET_MASTERViewSource.Source =Load_Asset_Mast ...
我的意思是你应该设置项目资源

hRMS_IT_ASSET_MASTERViewSource.ItemsSource =Load_Asset_Mast ...
hRMS_IT_ASSET_MASTERViewSource.Source =Load_Asset_Mast ...