Export (0) Print
Expand All

BindingSource::BindingComplete Event

Occurs when all the clients have been bound to this BindingSource.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
 event BindingCompleteEventHandler^ BindingComplete {
	void add (BindingCompleteEventHandler^ value);
	void remove (BindingCompleteEventHandler^ value);
}

The BindingComplete event occurs after all the clients, typically controls, have been bound to the current BindingSource. The handler of this event can take the appropriate action based on the success, error, or exceptions in the binding process, by examining the BindingCompleteState property of the BindingCompleteEventArgs parameter.

For more information about handling events, see Handling and Raising Events.

The following code example demonstrates handling the BindingComplete event. To run this code, paste it into an empty code file.

    // Represents a business object that throws exceptions when  
    // invalid values are entered for some of its properties. 
    public ref class Part
    {
    private:
        String^ name;
        int number;
        double price;

    public:
        Part(String^ name, int number, double price)
        {
            PartName = name;
            PartNumber = number;
            PartPrice = price;
        }

        property String^ PartName 
        {
            String^ get()
            {
                return name;
            }

            void set(String^ value)
            {
                if (value->Length <= 0)
                {
                    throw gcnew Exception(
                        "Each part must have a name.");
                }
                else
                {
                    name = value;
                }
            }
        }

        property double PartPrice 
        {
            double get()
            {
                return price;
            }

            void set(double value)
            {
                price = value;
            }
        }

        property int PartNumber 
        {
            int get()
            {
                return number;
            }

            void set(int value)
            {
                if (value < 100)
                {
                    throw gcnew Exception(
                        "Invalid part number." \
                        "Part numbers must be " \
                        "greater than 100.");
                }
                else
                {
                    number = value;
                }
            }
        }

    };

    ref class MainForm: public Form
    {
    private:
        BindingSource^ bindingSource;
        TextBox^ partNameTextBox;
        TextBox^ partNumberTextBox;
        TextBox^ partPriceTextBox;

    public:
        MainForm()
        {
            bindingSource = gcnew BindingSource;
            partNameTextBox = gcnew TextBox;
            partNumberTextBox = gcnew TextBox;
            partPriceTextBox = gcnew TextBox;

            //Set up the textbox controls. 
            this->partNameTextBox->Location = Point(82, 13);
            this->partNameTextBox->TabIndex = 1;
            this->partNumberTextBox->Location = Point(81, 47);
            this->partNumberTextBox->TabIndex = 2;
            this->partPriceTextBox->Location = Point(81, 83);
            this->partPriceTextBox->TabIndex = 3;

            // Add the textbox controls to the form 
            this->Controls->Add(this->partNumberTextBox);
            this->Controls->Add(this->partNameTextBox);
            this->Controls->Add(this->partPriceTextBox);

            // Handle the form's Load event. 
            this->Load += gcnew EventHandler(this, 
                &MainForm::OnMainFormLoad);
        }

    private:

        void OnMainFormLoad(Object^ sender, EventArgs^ e)
        {   
            // Set the DataSource of bindingSource to the Part type.
            bindingSource->DataSource = Part::typeid;

            // Bind the textboxes to the properties of the Part type, 
            // enabling formatting.
            partNameTextBox->DataBindings->Add(
                "Text", bindingSource, "PartName", true);
            partNumberTextBox->DataBindings->Add(
                "Text", bindingSource, "PartNumber", true);

            //Bind the textbox to the PartPrice value  
            // with currency formatting.
            partPriceTextBox->DataBindings->Add("Text", bindingSource, "PartPrice", true,
               DataSourceUpdateMode::OnPropertyChanged, nullptr, "C");

            // Handle the BindingComplete event for bindingSource and 
            // the partNameBinding.
            bindingSource->BindingComplete += 
                gcnew BindingCompleteEventHandler(this, 
                &MainForm::OnBindingSourceBindingComplete);
            bindingSource->BindingComplete += 
                gcnew BindingCompleteEventHandler(this, 
                &MainForm::OnPartNameBindingBindingComplete);

            // Add a new part to bindingSource.
            bindingSource->Add(gcnew Part("Widget", 1234, 12.45));
        }

        // Handle the BindingComplete event to catch errors and  
        // exceptions in binding process. 
        void OnBindingSourceBindingComplete(Object^ sender, 
            BindingCompleteEventArgs^ e)
        {
            if (e->BindingCompleteState == 
                BindingCompleteState::Exception)
            {
                MessageBox::Show(String::Format(
                    CultureInfo::CurrentCulture,
                    "bindingSource: {0}", e->Exception->Message));
            }

            if (e->BindingCompleteState == 
                BindingCompleteState::DataError)
            {
                MessageBox::Show(String::Format(
                    CultureInfo::CurrentCulture,
                    "bindingSource: {0}", e->Exception->Message));
            }
        }

        // Handle the BindingComplete event to catch errors and  
        // exceptions in binding process. 
        void OnPartNameBindingBindingComplete(Object^ sender, 
            BindingCompleteEventArgs^ e)
        {
            if (e->BindingCompleteState == 
                BindingCompleteState::Exception)
            {
                MessageBox::Show(String::Format(
                    CultureInfo::CurrentCulture,
                    "PartNameBinding: {0}", e->Exception->Message));
            }

            if (e->BindingCompleteState == 
                BindingCompleteState::DataError)
            {
                MessageBox::Show(String::Format(
                    CultureInfo::CurrentCulture,
                    "PartNameBinding: {0}", e->Exception->Message));
            }
        }
    };

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft