# VBForums CodeBank > CodeBank - C# >  Binding a Data Reader in Windows Forms

## jmcilhinney

VB version here.

I learned something new today so I felt the need to share it. It has always been the case that complex data-binding in Windows Forms requires an object that implements the _IList_ or _IListSource_ interface. Arrays implement _IList_, as does the _List<T>_ class. The _DataTable_ class implements _IListSource_ and _DataView_ implements _IList_. The _BindingList<T>_ and _BindingSource_ classes both implement _IList_. These are the most common types used as data sources for _ComboBoxes_, _DataGridViews_ and the like.

I generally recommend that people use a _BindingSource_ when binding, so bind their data to the _BindingSource_ and bind that to the control(s). In some cases it makes little difference but in others it can help and I have found such a way that it can help that I wasn't previously aware of. When binding read-only data from a database, I have generally recommended creating a data reader, loading the data into a _DataTable_ and then binding that to a _BindingSource_. That's because I assumed that a _BindingSource_ also required an _IList_ or _IListSource_, but that turns out not to be the case. In various cases, the _BindingSource_ will generate its own _IBindingList<T>_ and binding a data reader is one such case.

If you bind a data reader (_SqlDataReader_, _OleDbDataReader_, etc) to a _BindingSource_ then it will automatically generate an _IBindingList<System.Data.Common.DataRecordInternal>_. That list is similar to the _DataRowCollection_ from the _Rows_ property of a _DataTable_ or the _DataView_ from the _DefaultView_ property. You can index a _DataRecordInteral_ by column name or ordinal in the same way you can a _DataRow_ or _DataRowView_ but, unlike those other two, it only stores one version of the data, which makes it more efficient for read-only data. It also supports all the same _PropertyDescriptor_ functionality that a _DataGridView_ needs to automatically generate columns.

Here's an example of how I may have bound read-only data from a database before:

csharp Code:
// Create a new DataTable
var table = new DataTable();
 using (var connection = new SqlConnection("connection string here"))
using (var command = new SqlCommand("SELECT * FROM MyTable", connection))
{
    connection.Open();
     using (var reader = command.ExecuteReader())
    {
        // Populate the DataTable from the data reader
        table.Load(reader);
    }
}
 // Bind the DataTable
bindingSource1.DataSource = table;
 comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = bindingSource1;
 textBox1.DataBindings.Add("Text", bindingSource1, "Description");
 dataGridView1.DataSource = bindingSource1;
and here's how I might do it now:

csharp Code:
using (var connection = new SqlConnection("connection string here"))
using (var command = new SqlCommand("SELECT * FROM MyTable", connection))
{
    connection.Open();
     using (var reader = command.ExecuteReader())
    {
        // Bind the data reader and generate an IBindingList<DataRecordInternal>
        bindingSource1.DataSource = reader;
    }
}
 comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = bindingSource1;
 textBox1.DataBindings.Add("Text", bindingSource1, "Description");
 dataGridView1.DataSource = bindingSource1;
As you can see, the code changes very little but there is slightly less code and the data structures underneath will be less resource-intensive. In the first case, each item is a _DataRowView_ from the _DefaultView_ of the _DataTable_ while, in the second case, each item is  _DataRecordInternal_ from an _IBindingList<T>_. The user will see no difference and you see little difference as the developer but your app is a little bit more efficient.

----------

