如何将带有联接的查询绑定到 DataGridView?

发布于 2024-09-06 16:42:32 字数 758 浏览 4 评论 0原文

现在,我已经根据实体模型的对象上下文创建了一个新的对象数据源。然后,我创建了一个 BindingSource 和一个设置为此 BindingSource 的 DataGridView。

我可以添加绑定到 TraceLine 表中的数据的列。当我设置数据源时,我会看到这些列中的值。但是,我似乎无法从连接表中获取数据。如何将 DataGridView 绑定到具有联接的查询?

using (var entities = new MyEntities())
{
    var lines = from t in entities.Lines
                join m in entities.Methods on t.MethodHash equals m.MethodHash
                where t.UserSessionProcessId == m_SessionId
                select new
                {
                    m.Name,  // doesn't get displayed in DataGridView, but I want it to
                    t.Sequence,
                    t.InclusiveDuration,
                    t.ExclusiveDuration
                };

    dgvBindingSource.DataSource = lines;
}

Right now, I have created a new Object data source based off from an Object Context from the entity model. I then created a BindingSource and a DataGridView set to this BindingSource.

I can add columns which are bound to the data from the TraceLine table. When I set the DataSource, I see values in those columns. However, I can’t seem to get the data from the joined table. How do I bind a DataGridView to a query that has a join?

using (var entities = new MyEntities())
{
    var lines = from t in entities.Lines
                join m in entities.Methods on t.MethodHash equals m.MethodHash
                where t.UserSessionProcessId == m_SessionId
                select new
                {
                    m.Name,  // doesn't get displayed in DataGridView, but I want it to
                    t.Sequence,
                    t.InclusiveDuration,
                    t.ExclusiveDuration
                };

    dgvBindingSource.DataSource = lines;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

反目相谮 2024-09-13 16:42:32

一个可能的问题是,DataGridView 可能在设计时将其数据源设置为其中一种类型,但在运行时将其设置为具有额外成员的匿名类型。如果我记得,如果您在生成列后更改数据源,DataGridView 将不会重新生成列。

您可能需要将数据源设置为空,清除列集合,然后设置数据源。事实上,更好的想法是显式创建列而不是自动生成它们。

One possible issue is that the DataGridView may have its DataSource set at design time to one of the types but at runtime you're setting it to an anonymous type that has an extra member. If I recall, DataGridView won't re-generate the columns if you change the data source after columns have been generated.

You may need to set the data source to null, clear the column collection, then set the data source. In fact a better idea would be to create the columns explicitly instead of auto generating them.

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