Condividi tramite


How to: Bind a Windows Forms Control to a Type

When you are building controls that interact with data, you will sometimes find it necessary to bind a control to a type, rather than an object. This situation arises especially at design time, when data may not be available, but your data-bound controls still need to display information from a type's public interface. For example, you may bind a DataGridView control to an object exposed by a Web service and want the DataGridView control to label its columns at design time with the member names of a custom type.

You can easily bind a control to a type with the BindingSource control.

Example

The following code example demonstrates how to bind a DataGridView control to a custom type by using a BindingSource control. When you run the example, you'll notice the DataGridView has labeled columns that reflect the properties of a Customer object, before the control is populated with data. The example has an Add Customer button to add data to the DataGridView control. When you click the button, a new Customer object is added to the BindingSource. In a real-world scenario, the data might be obtained by a call to a Web service or other data source.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Class Form1
    Inherits Form

    Private bSource As New BindingSource()
    Private dgv As New DataGridView()

    Public Sub New()
        ' Bind the BindingSource to the DemoCustomer type.
        bSource .DataSource = GetType(DemoCustomer)

        ' Set up the DataGridView control.
        dgv.Dock = DockStyle.Fill
        Me.Controls.Add(dgv)

        ' Bind the DataGridView control to the BindingSource.
        dgv.DataSource = bSource 
    End Sub
End Class

' This simple class is used to demonstrate binding to a type.
Public Class DemoCustomer

    ' These fields hold the data that backs the public properties.
    Private birthDateValue As DateTime = DateTime.MinValue
    Private idValue As Guid = Guid.NewGuid()

    ' This is a property that represents a birth date.
    Public Property BirthDate() As DateTime
        Get
            Return Me.birthDateValue
        End Get
        Set(ByVal value As DateTime)
            If Value <> Me.birthDateValue Then
                Me.birthDateValue = Value
            End If
        End Set
    End Property

    ' This is a property that represents a customer ID.
    Public ReadOnly Property ID() As Guid
        Get
            Return Me.idValue
        End Get
    End Property
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    BindingSource bSource  = new BindingSource();
    DataGridView dgv = new DataGridView();

    public Form1()
    {
        // Bind the BindingSource to the DemoCustomer type.
        bSource.DataSource = typeof(DemoCustomer);

        // Set up the DataGridView control.
        dgv.Dock = DockStyle.Fill;
        this.Controls.Add(dgv);

        // Bind the DataGridView control to the BindingSource.
        dgv.DataSource = bSource  ;
    }
}

// This simple class is used to demonstrate binding to a type.
public class DemoCustomer
{
    // These fields hold the data that backs the public properties.
    DateTime birthDateValue = DateTime.MinValue;
    Guid idValue = Guid.NewGuid();

    // This is a property that represents a birth date.
    public DateTime BirthDate
    {
        get
        {
            return this.birthDateValue;
        }
        set
        {
            if (value != this.birthDateValue)
            {
                this.birthDateValue = value;
            }
        }
    }

    // This is a property that represents a customer ID.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }
}
#using <System.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Windows::Forms;

// This simple class is used to demonstrate binding to a type.
public ref class DemoCustomer
{
private:
   // These fields hold the data that backs the public properties.
   static DateTime birthDateValue = DateTime::MinValue;
   static Guid idValue = Guid::NewGuid();

public:
   property DateTime BirthDate 
   {
      // This is a property that represents a birth date.
      DateTime get()
      {
         return this->birthDateValue;
      }

      void set( DateTime value )
      {
         if ( value != this->birthDateValue )
         {
            this->birthDateValue = value;
         }
      }
   }

   property Guid ID 
   {
      // This is a property that represents a customer ID.
      Guid get()
      {
         return this->idValue;
      }
   }
};

ref class Form1: public Form
{
private:
   static BindingSource^ dc = gcnew BindingSource;
   static DataGridView^ dgv = gcnew DataGridView;

public:
   Form1()
   {
      // Bind the BindingSource to the DemoCustomer type.
      dc->DataSource = DemoCustomer::typeid;
      
      // Set up the DataGridView control.
      dgv->Dock = DockStyle::Fill;
      this->Controls->Add( dgv );
      
      // Bind the DataGridView control to the BindingSource.
      dgv->DataSource = dc;
   }
};

Compiling the Code

This example requires:

  • References to the System 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 (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

See Also

Reference

BindingNavigator
DataGridView
BindingSource

Other Resources

BindingSource Component