如何使用 BindingSources 将 DataObject 列表绑定到网格?

发布于 2024-08-26 19:28:48 字数 823 浏览 14 评论 0原文

在程序集中,我创建了一个如下所示的类:

[DataObject(true)]
public class A
{
 public int Foo{get;set;}

 [DataObjectMethod[DataObjectMethodType.Select)]
 public static List<A> GetAllA(string ConnectionString)
 {
   // return filled List<A>
 }
}

现在我想在 Winforms 下使用 Gridcontrol 显示此列表。我想到了 DataGrid。
虽然我来自 ASP.net,但我首先想到的是

this.dataGridView1.DataSource = A.GetAllA(ConnectionString)

Works,但我更喜欢使用 BindingSources 进行更好的数据绑定。 (因为我一直听说那就是要走的路)
我设法将 BindingSource 拖放到表单上并将 DataSource 属性设置为 A 类。
但是我可以在哪里设置 SelectMethod 及其参数呢?如果我将 dataGridView 的 DataSource 属性设置为 BindingSource,它只会显示一个空行。

这是正确的方法吗?是否只需要在向导中进行一些额外的单击,或者我是否需要阅读大量文档才能使其正常工作?

编辑:有没有办法实现自动绑定到我的选择方法?或者 BindingSource 仅支持映射列,但不实际绑定数据,这意味着我仍然需要设置 DataSource 属性?

In an assembly I created a class like the following:

[DataObject(true)]
public class A
{
 public int Foo{get;set;}

 [DataObjectMethod[DataObjectMethodType.Select)]
 public static List<A> GetAllA(string ConnectionString)
 {
   // return filled List<A>
 }
}

Now I want to display this List with a Gridcontrol under Winforms. I though of a DataGrid.
Though I'm coming from ASP.net I'd first think of

this.dataGridView1.DataSource = A.GetAllA(ConnectionString)

Works, but I'd prefer a better databinding with BindingSources. (Because I've always heard that thats the way to go)
I managed to drop a BindingSource onto the form and set the DataSource property to class A.
But where can I set the SelectMethod and its parameters? If I set DataSource property of the dataGridView to the BindingSource, it will only display an empty line.

Is this the right way to go? Will it only require some additional clicks in the wizard, or do I need to read tons of documentation to get this working?

Edit: Is there even a way to achieve automatically binding to my select method? Or does the BindingSource only supports mapping the columns, but not actually binding the data, meaning I'm required to set the DataSource property nevertheless?

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

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

发布评论

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

评论(2

一张白纸 2024-09-02 19:28:48

您需要创建一个数据源。单击“数据”菜单并选择“添加新数据源...”

在 Visual Studio 中连接到数据概述
http://msdn.microsoft.com/en-us /library/wxt2cwcc(VS.80).aspx

将您的应用程序连接到中的数据
数据库、Web 服务或对象,
运行数据源配置
向导

通过选择添加新数据源
数据源
窗口

Public Class A
    Private _field As String
    Public Property Field() As String
        Get
            Return _field
        End Get
        Set(ByVal value As String)
            _field = value
        End Set
    End Property
End Class

Public Class AListing
    Inherits List(Of A)
End Class
  • 添加数据源时使用AListing作为对象。适合提供导航的网格视图或详细信息表单。由您来填充它。
  • 添加数据源时使用A作为对象。当您只需要绑定到一个实例时,非常适合对话框。由您来填充它。

数据源只是帮助设计者配置数据绑定。您仍然需要填充对象。如果您不关心设计师的支持,直接打电话就可以了。使用 BindingSource 只允许您使用“数据表”之类的对象。使用您的示例,如果我使用 BindingSource,我可以处理 CurrentChanged 事件以进行任何其他处理。

this.dataGridView1.DataSource = A.GetAllA(ConnectionString);
//-or-
this.bindingSource1.DataSource = A.GetAllA(ConnectionString);

You need to create a DataSource. Click "Data" menu and select "Add New DataSource..."

Connecting to Data in Visual Studio Overview
http://msdn.microsoft.com/en-us/library/wxt2cwcc(VS.80).aspx

To connect your application to data in
a database, Web service, or object,
run the Data Source Configuration
Wizard

by selecting Add New Data Source from
the Data Sources
Window
.

Public Class A
    Private _field As String
    Public Property Field() As String
        Get
            Return _field
        End Get
        Set(ByVal value As String)
            _field = value
        End Set
    End Property
End Class

Public Class AListing
    Inherits List(Of A)
End Class
  • Use AListing as the object when adding a data source. Good for grid views or detail forms that provide navigation. It is up to you to populate it.
  • Use A as the object when adding a data source. Good for a dialog when you only need to bind to one instance. It is up to you to populate it.

A DataSource just helps the designer configure data binding. You still have to fill the objects. If you do not care about designer support, calling as you do is fine. Using a BindingSource just allows you to use an object like a "data table". Using your example, if I use a BindingSource, I could handle the CurrentChanged event for any additional processing.

this.dataGridView1.DataSource = A.GetAllA(ConnectionString);
//-or-
this.bindingSource1.DataSource = A.GetAllA(ConnectionString);
情栀口红 2024-09-02 19:28:48

让 A 类从配置文件中检索连接字符串,而不是作为 GetAllA 方法的参数。一旦您的方法没有参数,就可以在向导中选择它。

Have class A retrieve the connection string from the configuration file rather than as a parameter on the GetAllA method. Once your method has no parameters it should be possible to select it in the wizard.

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