How to: Bind a Windows Forms Control to a Factory Object


When you are building controls that interact with data, you will sometimes find it necessary to bind a control to an object or method that generates other objects. Such an object or method is called a factory. Your data source might be, for example, the return value from a method call, instead of an object in memory or a type. You can bind a control to this kind of data source as long as the source returns a collection.

You can easily bind a control to a factory object by using the BindingSource control.

The following example demonstrates how to bind a DataGridView control to a factory method by using a BindingSource control. The factory method is named GetOrdersByCustomerId, and it returns all the orders for a given customer in the Northwind database.

#using <System.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.EnterpriseServices.dll>
#using <System.Transactions.dll>
#using <System.Windows.Forms.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Data::Common;
using namespace System::Data::SqlClient;
using namespace System::Diagnostics;
using namespace System::Drawing;
using namespace System::Windows::Forms;

// This form demonstrates using a BindingSource to bind to a factory
// object.
public ref class Form1: public System::Windows::Forms::Form

   // This is the TextBox for entering CustomerID values.
   static TextBox^ customerIdTextBox = gcnew TextBox;

   // This is the DataGridView that displays orders for the
   // specified customer.
   static DataGridView^ customersDataGridView = gcnew DataGridView;

   // This is the BindingSource for binding the database query
   // result set to the DataGridView.
   static BindingSource^ ordersBindingSource = gcnew BindingSource;

      // Set up the CustomerID TextBox.
      this->customerIdTextBox->Dock = DockStyle::Bottom;
      this->customerIdTextBox->Text =
         L"Enter a valid Northwind CustomerID, for example: ALFKI,"

      L" then TAB or click outside the TextBox";
      this->customerIdTextBox->Leave += gcnew EventHandler(
         this, &Form1::customerIdTextBox_Leave );
      this->Controls->Add( this->customerIdTextBox );
      // Set up the DataGridView.
      customersDataGridView->Dock = DockStyle::Top;
      this->Controls->Add( customersDataGridView );
      // Set up the form.
      this->Size = System::Drawing::Size( 800, 800 );
      this->Load += gcnew EventHandler( this, &Form1::Form1_Load );

   // This event handler binds the BindingSource to the DataGridView
   // control's DataSource property.
   void Form1_Load(
      System::Object^ /*sender*/,
      System::EventArgs^ /*e*/ )
      // Attach the BindingSource to the DataGridView.
      this->customersDataGridView->DataSource =

   // This is a static factory method. It queries the Northwind
   // database for the orders belonging to the specified
   // customer and returns an IList.
   static System::Collections::IList^ GetOrdersByCustomerId( String^ id )
      // Open a connection to the database.
      String^ connectString = L"Integrated Security=SSPI;"
      L"Persist Security Info=False;Initial Catalog=Northwind;"
      L"Data Source= localhost";
      SqlConnection^ connection = gcnew SqlConnection;
      connection->ConnectionString = connectString;
      // Execute the query.
      String^ queryString = String::Format(
         L"Select * From Orders where CustomerID = '{0}'", id );
      SqlCommand^ command = gcnew SqlCommand( queryString,connection );
      SqlDataReader^ reader = command->ExecuteReader(
         CommandBehavior::CloseConnection );
      // Build an IList from the result set.
      List< DbDataRecord^ >^ list = gcnew List< DbDataRecord^ >;
      System::Collections::IEnumerator^ e = reader->GetEnumerator();
      while ( e->MoveNext() )
         list->Add( dynamic_cast<DbDataRecord^>(e->Current) );

      return list;

   // This event handler is called when the user tabs or clicks
   // out of the customerIdTextBox. The database is then queried
   // with the CustomerID in the customerIdTextBox.Text property.
   void customerIdTextBox_Leave( Object^ /*sender*/, EventArgs^ /*e*/ )
      // Attach the data source to the BindingSource control.
      this->ordersBindingSource->DataSource = GetOrdersByCustomerId(
         this->customerIdTextBox->Text );

   static void main()
      Application::Run( gcnew Form1 );

This example requires:

  • References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

BindingSource Component
How to: Bind a Windows Forms Control to a Type