如何使用 BindingSources 将 DataObject 列表绑定到网格?
在程序集中,我创建了一个如下所示的类:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个
数据源
。单击“数据”菜单并选择“添加新数据源...”在 Visual Studio 中连接到数据概述
http://msdn.microsoft.com/en-us /library/wxt2cwcc(VS.80).aspx
AListing
作为对象。适合提供导航的网格视图或详细信息表单。由您来填充它。A
作为对象。当您只需要绑定到一个实例时,非常适合对话框。由您来填充它。数据源只是帮助设计者配置数据绑定。您仍然需要填充对象。如果您不关心设计师的支持,直接打电话就可以了。使用 BindingSource 只允许您使用“数据表”之类的对象。使用您的示例,如果我使用 BindingSource,我可以处理 CurrentChanged 事件以进行任何其他处理。
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
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.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.
让 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.