绑定数据源但显示其他内容

发布于 2024-11-17 22:09:40 字数 341 浏览 4 评论 0原文

我有很多地方需要将表单控件直接绑定到支持数据库。我正在使用 LINQ to DataSet 来执行此操作。

例如,我有一个组合框,其中的条目由数据库查询填充。问题当然是我的所有数据都使用数字 ID,我需要执行表查询以将其转换为用户可读的选项。

通常我会加入第二个表并使用组合框 DisplayMember 指向用户可读的字符串列。这不起作用,因为在查询上使用联接或任何投影后,您(可以理解)无法将该查询转换为 DataView。

很难相信每个使用 DataView 的人都会遇到这个问题。有没有什么方法可以覆盖我的表单控件的行为,使它们显示其值的函数?就像如果它们的值是 v,那么它们会显示 SomeMethod(v)?

I have quite a few places where I need to bind form controls directly to a backing database. I'm using LINQ to DataSet to do this.

For example, I have a ComboBox with entries filled in by a database query. The problem is of course that all of my data uses numerical IDs and I need to do a table query to translate this into user-readable options.

Normally I would just join the second table and use the combobox DisplayMember to point at the user-readable string column. This doesn't work because after using join or any projections on the query you're (understandably) unable to convert that query into a DataView.

It's hard to believe that this problem isn't run into by everyone who uses DataView. Is there any way to override my form controls' behavior to make them display a function of their value? Like if their Value is v, then they display SomeMethod(v)?

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

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

发布评论

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

评论(2

邮友 2024-11-24 22:09:40

我不相信通过 DisplayMember 可以实现您想要完成的任务。它是一种财产,这就是它的设计方式。但是,还有一些其他方法可以实现您想要的目的。

1)您可以连接到组合框的格式 事件。这是将数据绑定项更改为人类可读项以进行显示的理想位置,并且确实正是您想要的。

2) 您可以改用 LINQ to SQL 类,并重写部分类中的 .ToString() 部分以按照您想要的方式显示数据。当您对 LINQ to SQL 对象进行数据绑定时,组合框将显示该对象的字符串值。

由于您已经在使用 LINQ to DataSet,因此我只需连接到 Format 事件。

I don't believe what you are trying to accomplish is possible by means of DisplayMember. Its a property, that's how it was designed. However, there are a few other means to accomplish what you want.

1) You could wireup to the ComboBox's Format event. This is the ideal place to change your databound items into human-readable items for display, and really is exactly what you want.

2) You could use LINQ to SQL classes instead, and override the .ToString() portions in the partial classes to display the data how you want. When you databind to a LINQ to SQL object the ComboBox will display the string value of that object.

Since you are already using LINQ to DataSet, I would just wireup to the Format event, though.

生寂 2024-11-24 22:09:40

我对这些事情有一个非常简单的方法 - 如果它不能很好地发挥作用,只需发明一个视图模型,即一个旨在使域模型和 UI 之间的映射顺利进行的对象布局。这也可以鼓励您以视图模型(而不是域模型)的正确形状填充数据;这通常可以减少获取的列(有时是行)的数量,并且(也许更重要的是)避免多次访问数据库(通过一次获取准确正确的数据)击中,形状正确)。

然后,您可以在视图模型中使用简单的数据绑定技术,因为您想要的数据现在可以直接用作视图模型上的第一级值。

I have a pretty simple approach to these things - if it doesn't play nicely, just invent a view-model, i.e. an object layout designed to make mapping between your domain model and the UI plain sailing. This can also encourage you to populate your data in the correct shape for the view-model, rather than for the domain-model; and this often has decent savings in terms of reducing the number of columns (and sometimes rows) fetched, and (perhaps more importantly) avoiding multiple trips to the DB (by fetching exactly the right data, in one hit, in the right shape).

From a view-model, you can then use simple data-binding techniques, as the data you want is now directly available as first-level values on the view-model.

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