1

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)?

4

2 に答える 2

0

私はこれらのことに対して非常に単純なアプローチをとっています-うまく機能しない場合は、ビューモデル、つまりドメインモデルとUIのプレーンセーリングの間のマッピングを作成するように設計されたオブジェクトレイアウトを発明するだけです。これにより、ドメインモデルではなく、ビューモデルの正しい形でデータを入力することもできます。これにより、フェッチされる列(場合によっては行)の数が減り、(おそらくもっと重要なことですが)DBへの複数のトリップが回避される(1回のヒットで正しい形で正確に正しいデータをフェッチする)という点で、かなりの節約になることがよくあります。。

ビューモデルから、必要なデータがビューモデルの第1レベルの値として直接利用できるようになったため、簡単なデータバインディング手法を使用できます。

于 2011-06-29T20:14:35.473 に答える
0

あなたが達成しようとしていることが DisplayMember によって可能であるとは思えません。そのプロパティは、それがどのように設計されたかです。ただし、目的を達成するための手段が他にもいくつかあります。

1) ComboBox のFormatイベントに接続できます。これは、データバインドされたアイテムを表示用に人間が読めるアイテムに変更するのに理想的な場所であり、まさにあなたが望むものです。

2) 代わりに LINQ to SQL クラスを使用し、部分クラスの .ToString() 部分をオーバーライドして、必要な方法でデータを表示できます。LINQ to SQL オブジェクトにデータバインドすると、ComboBox にそのオブジェクトの文字列値が表示されます。

ただし、既に LINQ to DataSet を使用しているため、Format イベントに接続するだけです。

于 2011-06-29T21:20:49.623 に答える