Bringing the Data Source and the Form Together

Ok, so we have the data source and the form we’ve created. The data source contains the Clients table, and the form is a copy of Form1 that we have called ClientList. Let’s go ahead and change the form’s Text property of ClientList to “Client List.” Now we can drag the Clients table from the data source to the Data Grid View on our form, like this:

Then our form will look like this:

Visual Studio does a whole lot behind the scenes when we drop a datasource onto a form. If there is no data grid view, it will create one, but if there is one, it will add the columns without changing other properties previously set. Verify this. All the properties you set on the DGV should still be there.

Visual Studio also creates some data objects when you drop a data source onto a form. It took me a while to understand how Microsoft’s database handling works. We start with the data set. This is where the tables we created in the visual data designer are stored. The table adapter object does not hold the table. The table is in the data set. The table adapter is just a tool to manipulate the table in the data set. Thus, you use the table adapter’s Fill method to fill the table that is in the data set.

The binding source is the connection between the data source (the table that is in the data set) and the form controls that are on the screen. When the contents of the data change in the data source, the binding source automatically updates the screen, and likewise, when the contents of the screen change, the binding source updates the contents of the data source (not automatically, but easily enough). When developing with forms, you generally use the binding source to manipulate the data. If you need to insert a record, you insert it in the binding source, and let the binding source do its thing.

The table adapter manager is only needed when you update multiple related tables. By calling one of its methods, you ensure that the tables update in the proper order, without breaking relational rules. In this case, we will delete it because we are only using one table.

Finally, Visual Studio has added code not just to the designer file but to our own code file as well. Open ClientList.cs and see.

namespace TheBooks
{
    public partial class ClientList : Form
    {
        public ClientList()
        {
            InitializeComponent();
        }

        private void ClientList_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dSClients.Clients' table. You can move, or remove it, as needed.
            this.clientsTableAdapter.Fill(this.dSClients.Clients);
        }
    }
}

Visual Studio added the TODO line and the line following it. You can see how the table adapter is used to fill a table in data set.

Leave a Reply

Your email address will not be published. Required fields are marked *