当 source 是 IQueryable 对象时访问 bindingsource.Current 的内容
目前,我的存储库返回一个 IQueryable
对象,该对象列出了我的数据库中的数据,并将其绑定到 BindingSource
以在网格中使用:
public void BindTo(IQueryable elements)
{
BindingSource source = new BindingSource();
source.CurrentChanged += new EventHandler(source_CurrentChanged);
source.DataSource = elements;
elementNavigator.BindingSource = source;
elementGridView.DataSource = source;
}
这非常有用。然而,当用户单击网格中的一行时,我想做一些事情。我正在努力识别用户正在选择的元素。我有以下内容:
在我看来:
private void source_CurrentChanged(object sender, EventArgs e)
{
_presenter.ElementChanged(sender, e);
}
在我的演示者中:
public void ElementChanged(object sender, EventArgs e)
{
BindingSource source = (BindingSource)sender;
// Here I need to get the ID of the selected element in the source.Current property.
// HOW?
}
这似乎工作正常 - 我可以在调试时看到 source.Current
包含数据:
? source.Current
{ BodyId = 1, IsInUse = true, IsValid = true, CreateDate = {04/07/2006 09:31:59}, LastUpdateDate = {04/07/2006 09:31:59}, StatusDescShort = "Exist" ... }
BodyId: 1
CreateDate: {04/07/2006 09:31:59}
IsInUse: true
IsValid: true
LastUpdateDate: {04/07/2006 09:31:59}
StatusDescShort: "Exist"
但我不知所措访问 BodyId
的值。我有一种感觉,我在这里错过了一些非常明显的东西(不是第一次)。
Currently my repository returns an IQueryable
object that lists the data from my DB and I bind this to a BindingSource
for use in a grid:
public void BindTo(IQueryable elements)
{
BindingSource source = new BindingSource();
source.CurrentChanged += new EventHandler(source_CurrentChanged);
source.DataSource = elements;
elementNavigator.BindingSource = source;
elementGridView.DataSource = source;
}
This works great. However I am wanting to do some stuff when a user clicks an row in the grid. I'm struggling to identify the element that the user is selecting. I have the following:
In my view:
private void source_CurrentChanged(object sender, EventArgs e)
{
_presenter.ElementChanged(sender, e);
}
In my presenter:
public void ElementChanged(object sender, EventArgs e)
{
BindingSource source = (BindingSource)sender;
// Here I need to get the ID of the selected element in the source.Current property.
// HOW?
}
This seems to work ok - and I can see when debugging that source.Current
contains the data:
? source.Current
{ BodyId = 1, IsInUse = true, IsValid = true, CreateDate = {04/07/2006 09:31:59}, LastUpdateDate = {04/07/2006 09:31:59}, StatusDescShort = "Exist" ... }
BodyId: 1
CreateDate: {04/07/2006 09:31:59}
IsInUse: true
IsValid: true
LastUpdateDate: {04/07/2006 09:31:59}
StatusDescShort: "Exist"
but I am at a loss as how I might access the value of BodyId
. I've a feeling I'm missing something really obvious here (not the first time).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很惊讶你没有使用像
IQueryable
这样的东西。因为这样就可以简单地进行转换:
source.Current as MYType
并且可能消除中间的 DataRowView
I'm surprised you're not using something like
IQueryable<MyType>
.Because then it would be a simple matter of casting:
source.Current as MYType
And maybe eliminate an in-between DataRowView