TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DataSource Example

This C# tutorial shows how to use DataSource. With this property, the code updates elements in a ListBox.

DataSource. The DataSource property allows data binding on Windows Forms controls.

With it we bind an array to a ListBox on the screen—and display all the strings. As changes are made to the List, we update the control on the screen.

ListBox

Example. First, your ListBox control—or any different kind of control you may have—has a DataSource property that can be assigned a value. You can assign a collection to it. Collections include arrays, Lists and DataTables.

Example that sets DataSource property: C#

public partial class MainWindow : Form
{
    List<string> _sideList = new List<string>();

    public MainWindow()
    {
	InitializeComponent();
	sideListBox1.DataSource = _sideList;
    }
}

This example has a List collection field. The above Form, which is called MainWindow, has a member variable of the List generic type. This will be where we want to store the strings that will show on the ListBox.

Note: The constructor and the partial class and InitializeComponent were autogenerated by the Designer.

Partial

In the constructor: It assigns to the DataSource. The DataSource property is assigned to the List member variable.

Constructor

Example 2. The point here is that we want to add items to the ListBox. We can add items to the bottom or the top, or anywhere in between. A good effect is adding items to the top, so they are easily seen.

Next: Here is a custom method that prepends a string to the top of the control, and then makes it display.

List Insert

Example that uses BindingContext: C#

private void AddListLine(string lineIn)
{
    // ... Insert the string at the front of the List.
    _sideList.Insert(0, lineIn);

    // ... Force a refresh of the ListBox.
    ((CurrencyManager)sideListBox1.BindingContext[_sideList]).Refresh();
}

The method receives a string and then Inserts it to the start of the member List, and then forces the ListBox to refresh. The syntax at the end does a cast and a lookup to get the CurrencyManager and then Refreshes the ListBox.

Tip: In the CurrencyManager type, the word Currency refers to current. It is used to ensure a control is current.

Example 3. So far we have seen the DataSource assignment and then the method AddListLine that inserts a string at the top of the ListBox. This setup allows us to send a string to the method. It will show up at the top of the ListBox.

Example that calls method: C#

void backgroundWorker1_RunWorkerCompleted(object sender,
    RunWorkerCompletedEventArgs e)
{
    // ... Show some results from the BackgroundWorker task.
    AddListLine("5000 games: " + (string)e.Result);
}

 

Summary. We used the DataSource property on a ListBox control in Windows Forms. By combining one List, DataSource and ListBox, and using CurrencyManager, we have a ListBox that will show the newest result on the top.

Thus: These properties together result in an interface that is versatile and easily maintained.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf