使用 DataGridViewRow.DataBoundItem - 转换为可枚举对象?
我有一个带有
myGridView.DataSource = GetSomeData()
// method called
public IQueryable GetSomeData()
{
var source = from r in records
select r;
return source; // made correction from 'r' to 'source'
}
GetSomeData()
的 DataGridView 按预期填充 DataGridView。用户将选择要编辑的行,然后将行数据传递到表单。由于DataGridViewRow.DataBoundItem
是匿名类型,我如何传递DataBoundItem
?
我一半期望我的 DataBoundItem 将是 IQueryable - 不正确。以下是来自调试器的 DataBoundItem 属性信息:
DataBoundItem { 客户ID = "3133", 姓氏 =“史密斯”,名字 = "约翰", 帐号 = "JS3133", ActiveYN = True } <<匿名类型>
一旦数据传递到新表单,我想做一些类似的事情:
txtFName.Text = SomeEnumeratedObject["First_Name"];
txtLName.Text = SomeEnumeratedObject["Last_Name"];
关于如何做到这一点有什么想法吗?在我看来,如果新表单上的控件能够以某种方式绑定到 SomeEnumeratedObject
,那就更好了。
是否可以使用 LINQ 查询 DataBoundItem
?
编辑:
更改的方法:
public DataView GetSomeData()
{
var source = from r in records
select r;
DataTable table = ToDataTable(myContext, source);
return new DataView(table);
}
请参阅完整的解决方案此处。
I have a DataGridView with
myGridView.DataSource = GetSomeData()
// method called
public IQueryable GetSomeData()
{
var source = from r in records
select r;
return source; // made correction from 'r' to 'source'
}
GetSomeData()
fills the DataGridView as expected. The user will select a row to edit which then passes the row data to a form. Since the DataGridViewRow.DataBoundItem
is an anonymous type, how can I pass DataBoundItem
?
I half expected that my DataBoundItem would be IQueryable - incorrect. Here is the info from the debugger on the DataBoundItem property:
DataBoundItem { CustomerID = "3133",
Last_Name = "Smith", First_Name =
"John", AccountNumber = "JS3133",
ActiveYN = True } < Anonymous Type >
Once the data is passed to the new form, I would like to do something like:
txtFName.Text = SomeEnumeratedObject["First_Name"];
txtLName.Text = SomeEnumeratedObject["Last_Name"];
Any ideas on how I can do this? It would be even better, IMO, if the controls on the new form could some how be bound to SomeEnumeratedObject
.
Would it possible to query the DataBoundItem
with LINQ?
Edit:
Changed method:
public DataView GetSomeData()
{
var source = from r in records
select r;
DataTable table = ToDataTable(myContext, source);
return new DataView(table);
}
See complete solution here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
记录中包含什么类型的对象?您应该能够将 DataGridViewRow.DataBoundItem 转换为任何类型的对象记录。
假设记录是 Customer 对象的列表。你应该能够:
如果那不可能,那么我认为你将不得不使用反射:
What type of objects are in records? You should be able to cast DataGridViewRow.DataBoundItem to whatever type of object records holds.
Say records is a list of Customer objects. You should be able to go:
If that isn't possible, then I think you will have to use reflection:
您可以将其作为动态传递并以这种方式访问属性:
需要考虑的一件事是匿名类型的属性是只读的,因此您的用户将无法以这种方式编辑值。来自 C# 语言规范:
您应该考虑为此声明一个类型,而不是使用匿名类型。这样,您还可以受益于智能感知,并能够毫无问题地重命名/重构您的属性。
You could pass it as
dynamic
and access the properties that way:One thing to consider is that properties on anonymous types are read only, so your user will not be able to edit the values that way. From the C# Language Spec:
You should consider declaring a type for this instead of using an anonymous type. That way you'll also get the benefit of intellisense and be able to rename/refactor your properties without any problems.
kevev22 和 kevev22 adrift 有正确的想法:创建一个类来保存数据。由于我可以将 IQueryable 结果集转换为 DataTable,因此这样做才有意义:
现在:
并且,当选择数据网格行时,您可以将
DataBoundItem
转换为DataRow
。仅供参考 - 将 IQueryable 结果集转换为 DataTable:
Both kevev22 & adrift had the right idea: create a class to hold the data. Since I can transform an IQueryable resultset to a DataTable, it only made sense to do that:
Now:
And, when a datagrid row is selected, you can cast the
DataBoundItem
toDataRow
.FYI - to transform an IQueryable resultset to a DataTable: