2

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

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

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

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

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

4

2 回答 2

1

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

在 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数据源时用作对象。当您只需要绑定到一个实例时,它适用于对话框。填充它取决于您。

DataSource 只是帮助设计者配置数据绑定。您仍然必须填充对象。如果你不关心设计师的支持,你可以打电话。使用 BindingSource 只允许您使用像“数据表”这样的对象。使用您的示例,如果我使用 BindingSource,我可以处理 CurrentChanged 事件以进行任何其他处理。

this.dataGridView1.DataSource = A.GetAllA(ConnectionString);
//-or-
this.bindingSource1.DataSource = A.GetAllA(ConnectionString);
于 2010-03-26T01:16:18.030 回答
0

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.

于 2010-03-26T00:34:40.220 回答