views:

205

answers:

2

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?

A: 

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.

Morten Mertner
Removing the connection string as parameter still does not give me the ability to select the selectmethod in the wizard.
citronas
It is still a good idea to remove the connection string as a parameter.
AMissico
+1  A: 

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);
AMissico
Already did that. The referenced assembly is up to date and there I still can't select the select method.
citronas
Ah, I got my WinForms and ASP.NET mixed up. Sorry. I updated my answer.
AMissico