当 source 是 IQueryable 对象时访问 bindingsource.Current 的内容

发布于 2024-10-09 14:49:52 字数 1345 浏览 1 评论 0原文

目前,我的存储库返回一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2024-10-16 14:49:52

我很惊讶你没有使用像 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文