JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



BindingSource



example0

The BindingSource control component is found in the System.Windows.Forms namespace within the System.Windows.Forms.dll. In this blog post I am referring to the BindingSource control that is available in C# .NET-Core (.NET 6.0) with Visual Studio 2022.

The BindingSource component can be found in the Controls Toolbox window panel within the Forms editor screen. Even though the BindingSource component is located in the Controls Toolbox, it is best to think of this object as a component and not as a control simply because it has no graphical user interface, and therefore it is not visible on the form itself.

The purpose and function of a BindingSource component is to encapsulate the data source for a form. It simplifies binding controls on form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. This is accomplished by attaching the BindingSource component to your data source using the DataSource property. For more complex binding scenarios you can optionally set the DataMember property to a specific list or column in the data source, and then bind controls to the BindingSource component. Once accomplished then all further interactions with the data is performed with calls to the BindingSource component.

Additionally, the BindingSource component can act as a strongly typed data source. You can use the Add method to add an item to the BindingSource component, or set the DataSource property to a list (data collection within the hosted data source), single object, or type. You can also use the BindingSource component to bind controls to a factory object.

The BindingSource component provides members for accessing the underlying data. The current item (single element) can be retrieved through the Current property, and the entire list (data collection) can be retrieved through the List property. Editing operations are supported on the current item through Current and the RemoveCurrent, EndEdit, CancelEdit, Add and AddNew methods. Although currency management is handled automatically for all underlying data source types, this class exposes a number of events, such as CurrentItemChanged and DataSourceChanged, that allow for further customization.

Data sources that are bound to a BindingSource component can also be navigated and managed with the BindingNavigator class, which provides a VCR-like user interface (UI) for navigating items within a list. Although BindingNavigator can be bound to any data source, it was designed to integrate with a BindingSource component through its BindingNavigator.BindingSource property.

The default property for the BindingSource class is DataSource, and the default event is CurrentChanged.


Example Source Code

This example uses a BindingSource control, along with a Button control, a ListBox control, a TextBox control, and a MessageBox object.

To add the BindingSource component to your form, you can double click on its name (i.e. BindingSource) as listed in the Toolbox window panel within the Form editor window. Alternatively, you can single click on it and then drag and drop it onto your form. Once it is added, it will appear in a window located below the form, since it has no user interface and will not actually appear on the form itself.

Once it is added, then you can select it in the Form editor and view and adjust its property values in the Properties window.

In the example below, using the Form editor I added to the form a Button control and altered its Text property to show "Search" as well as added a Click event method by double clicking on the Button object, a TextBox control and altered its Text property to give it a value of "Windings", and a ListBox control. Then in the source code I added a new bindable class called "MyFontList". I then populated the fonts list with system fonts, and bound it to the ListBox control by using the BindingSource component. In the Button's Click event I attempt to perform a search for the user provided font name as listed in the TextBox control, and then message the user with results feedback accordingly:

example1

 
// Copyright 2024 T&J Divisions, LLC	
// All Rights Reserved
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            // populate the data list with font names
            MyDataList fonts = new MyDataList();
            for (int i = 0; i < FontFamily.Families.Length; i++)
            {
                if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
                {
                    fonts.Add(new Font(FontFamily.Families[i], 11.0F, FontStyle.Regular));
                }
            }

            // bind the data list to the listbox
            bindingSource1 = new BindingSource();
            bindingSource1.DataSource = fonts;
            listBox1.DataSource = bindingSource1;
            listBox1.DisplayMember = "Name";
        }

        // method callback for the buttons Click event
        private void button1_Click(object? sender, EventArgs e)
        {
            // perform search using binding
            if (bindingSource1.SupportsSearching != true)
            {
                MessageBox.Show("Cannot search the list.");
            }
            else
            {
                int foundIndex = bindingSource1.Find("Name", textBox1.Text);
                if (foundIndex > -1)
                {
                    listBox1.SelectedIndex = foundIndex;
                }
                else
                {
                    MessageBox.Show("Font was not found.");
                }
            }
        }
    }

    // custom bindable fonts list
    public class MyDataList : BindingList<Font>
    {
        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }
        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            // Ignore the prop value and search by family name.
            for (int i = 0; i < Count; ++i)
            {
                if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
                {
                    return i;
                }
            }
            return -1;
        }
    }
}

When you run the above examples then you should see something similar to the following:

example3
example4
example5

Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved