将 IQueryable 转换为 ObservableCollection,必须知道如何
问题是为什么observable集合会出现空引用呢?当我尝试将项目添加到 obscollection 时,会发生错误。
注意:我必须知道如何将数据网格中存在的所有项目提供给可观察的集合。
错误:
未将对象引用设置为对象的实例。
行:-“好。添加(temp_table);”在按钮中单击一下
.xaml.cs
Web.DomainService1 oservice = new Web.DomainService1();
public static ObservableCollection<Web.EMP_TABLE> good = new ObservableCollection<Web.EMP_TABLE>();
public Home()
{
InitializeComponent();
this.Title = ApplicationStrings.HomePageTitle;
EntityQuery<Web.EMP_TABLE> q = oservice.GetEMP_TABLE_OBVQuery();
LoadOperation<Web.EMP_TABLE> l = oservice.Load(q);
dataGrid1.ItemsSource = l.Entities;
}
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
ObservableCollection<Web.EMP_TABLE> good =
dataGrid1.ItemsSource as ObservableCollection<Web.EMP_TABLE>;
Web.EMP_TABLE temp_table = new Web.EMP_TABLE();
temp_table.SALARY = "new_sal";
temp_table.EMP_NAME = "new_name";
temp_table.EMP_NO = "new_num";
good.Add(temp_table);
}
.xaml
<sdk:DataGrid AutoGenerateColumns="True" Height="116" Name="dataGrid2" Width="539" />
DomainService 函数
public ObservableCollection<EMP_TABLE> GetEMP_TABLE_OBV()
{
var value = from c in this.ObjectContext.EMP_TABLE
select c;
ObservableCollection<EMP_TABLE> result = new ObservableCollection<EMP_TABLE>(value);
return result;
}
The question is why does the null reference occur for the observable collection? The error occurs when I try to add item to the obscollection.
Note: I have to know how to give all items present in datagrid to observable collection.
Error:
Object reference not set to an instance of an object.
line:- "good.Add(temp_table);" in button one click
.xaml.cs
Web.DomainService1 oservice = new Web.DomainService1();
public static ObservableCollection<Web.EMP_TABLE> good = new ObservableCollection<Web.EMP_TABLE>();
public Home()
{
InitializeComponent();
this.Title = ApplicationStrings.HomePageTitle;
EntityQuery<Web.EMP_TABLE> q = oservice.GetEMP_TABLE_OBVQuery();
LoadOperation<Web.EMP_TABLE> l = oservice.Load(q);
dataGrid1.ItemsSource = l.Entities;
}
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
ObservableCollection<Web.EMP_TABLE> good =
dataGrid1.ItemsSource as ObservableCollection<Web.EMP_TABLE>;
Web.EMP_TABLE temp_table = new Web.EMP_TABLE();
temp_table.SALARY = "new_sal";
temp_table.EMP_NAME = "new_name";
temp_table.EMP_NO = "new_num";
good.Add(temp_table);
}
.xaml
<sdk:DataGrid AutoGenerateColumns="True" Height="116" Name="dataGrid2" Width="539" />
The DomainService function
public ObservableCollection<EMP_TABLE> GetEMP_TABLE_OBV()
{
var value = from c in this.ObjectContext.EMP_TABLE
select c;
ObservableCollection<EMP_TABLE> result = new ObservableCollection<EMP_TABLE>(value);
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用“as”而不是(TYPE)进行强制转换时,如果强制转换失败,则不会引发异常。您的变量只是设置为 null。
你的演员阵容失败了。
When you use "as" instead of (TYPE) to cast, no exception is thrown if the cast fails. Your variable is simply set to null.
Your cast is failing.
在您编辑和阅读评论后,我不明白这应该如何工作:
正如其他人所指出的,你的演员阵容失败了。
你说,你传递了一个ObservableCollection,但实际上,你没有这样做,看看这里:
在你的构造函数中:
l.Entities
很可能是一个IQueryable
,而不是ObservableCollection
稍后,你尝试转换
dataGrid1.ItemsSource
,这是失败的。您向我们展示了返回
ObservableCollection
的GetEMP_TABLE_OBV
代码,但我没有看到该方法的任何用法。After your edit and reading of the comments, I don't see, how this should work:
As others have pointed out, your cast is failing.
You say, you pass an ObservableCollection, but in fact, you don't do it, have a look here:
In your ctor:
l.Entities
is most likely anIQueryable
, not anObservableCollection
Later, you are trying to cast
dataGrid1.ItemsSource
, which is failing.You showed us the code of
GetEMP_TABLE_OBV
which returns anObservableCollection
, but I don't see any usage of that method.datagrid1.ItemSource 不是 ObservableCollection,因此您的转换失败,因此 good 为 null。
您必须检查 datagrid1.ItemSource 实际保存的类型(它是 LoadOperation 类型上的 Entities 属性的类型)并转换为该类型
datagrid1.ItemSource is not an ObservableCollection so your cast fails and good is therefor null.
You have to inspect what type datagrid1.ItemSource actually hold (it is the type of the Entities property on the LoadOperation type ) and cast to that type