什么 WinForm 控件可以绑定列表(T)?

发布于 2024-07-26 11:28:05 字数 1171 浏览 3 评论 0原文

我一直致力于让我的项目保持面向对象。 目前,我正在使用一个 .DLL,它将所有应用程序的类提供给 WinForms 项目作为表示层。

例如,我的 .DLL 将返回一个 SortableBindingList(Of T) 到表单中的代码。 SortableBindingList(Of T) 来自此处。 我们假设有一个 SortableBindingList(Of Product)。 假设 .DLL 的函数 Services.Products.GetList() 返回一个 SortableBindingList(Of Product),我可以轻松地执行此操作:

DataGridView1.DataSource = Services.Products.GetList()

现在,DataGridView 已正确填充了我的产品列表。 美好的。 但是,没有 .SelectedItem 属性可以返回在 DataGridView 中选择的对象:

' Doesn't exist!
Dim p As Product = DataGridView1.SelectedItem
' Need to make another DB call by getting the Product ID 
' from the proper Cell of the DataGridView ... yuck!

但是,ComboBox 或 ListBox 实际上确实完整地存储并返回了我的 Product 对象:

' Valid!
ComboBox1.DataSource = Services.Products.GetList()
Dim p as Product = ComboBox1.SelectedItem

但是另一个... ComboBox 和 ListBox 没有显示 Product 对象的所有字段,仅显示 DisplayMember 属性的值。

VB.NET 2008 中是否有一个我所缺少的很好的控件,它为我提供了我想要的面向对象的功能,该功能将实际显示整个对象的字段,并在用户选择时返回该对象? 我不知道为什么不会有。

I have been working on keeping things object oriented for my project. Currently, I'm using a .DLL which supplies all of the app's classes to the WinForms project acting as the presentation layer.

My .DLL will, for example, return a SortableBindingList(Of T) to code in a form. The SortableBindingList(Of T) comes from here. Let's assume a SortableBindingList(Of Product). Assuming that the .DLL's function Services.Products.GetList() returns a SortableBindingList(Of Product), I can easily do this:

DataGridView1.DataSource = Services.Products.GetList()

Now, the DataGridView is properly populated with my list of Products. Fine. However, there is no .SelectedItem property which gives me back my object which was selected in the DataGridView:

' Doesn't exist!
Dim p As Product = DataGridView1.SelectedItem
' Need to make another DB call by getting the Product ID 
' from the proper Cell of the DataGridView ... yuck!

However, a ComboBox or a ListBox does in fact store and return my Product objects intact:

' Valid!
ComboBox1.DataSource = Services.Products.GetList()
Dim p as Product = ComboBox1.SelectedItem

Another however ... the ComboBox and ListBox do not show all of the fields of the Product object, only the value of the DisplayMember property.

Is there a nice control in VB.NET 2008 that I am just missing, which gives me the object oriented functionality that I want which will actually display an entire object's fields and also return that object back when selected by the user? I'm at a loss as to why there wouldn't be.

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

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

发布评论

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

评论(1

感情旳空白 2024-08-02 11:28:05

听起来您正在寻找 DataGridView 的 SelectedRows 属性。 您应该能够将其用于您所描述的内容。

您可以使用它来获取 DataBoundItem,然后将其转换为原始类。 假设我有一个绑定的 Product 对象列表,我会使用类似的内容:

Dim p As Product = CType(dataGridView1.SelectedRows(0).DataBoundItem, Product)
MessageBox.Show(p.Name & " " & p.Price)

如果选择整行,则此方法有效,否则您可能会得到空引用异常。 在这种情况下,您可以通过以下方式获取当前选定单元格的 RowIndex:

dataGridView1.SelectedCells(0).RowIndex

所以现在看起来像:

If dataGridView1.SelectedCells.Count > 0 Then
    Dim index as Integer = dataGridView1.SelectedCells(0).RowIndex
    Dim p As Product = CType(dataGridView1.SelectedRows(index).DataBoundItem, Product)
    MessageBox.Show(p.Name & " " & p.Price)
End If

编辑: 更新为 VB.NET

It sounds like you're looking for the DataGridView's SelectedRows property. You should be able to use that for what you're describing.

You use it to get the DataBoundItem then cast that to your original class. Let's say I had a list of Product objects bound, I would use something like:

Dim p As Product = CType(dataGridView1.SelectedRows(0).DataBoundItem, Product)
MessageBox.Show(p.Name & " " & p.Price)

This works if the entire row is selected, otherwise you could get a null reference exception. In that case you could get the RowIndex of the currently selected cell via:

dataGridView1.SelectedCells(0).RowIndex

So all together this now looks like:

If dataGridView1.SelectedCells.Count > 0 Then
    Dim index as Integer = dataGridView1.SelectedCells(0).RowIndex
    Dim p As Product = CType(dataGridView1.SelectedRows(index).DataBoundItem, Product)
    MessageBox.Show(p.Name & " " & p.Price)
End If

EDIT: updated to VB.NET

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