Data Binding


An enhanced graphical user interface is definitely user friendly, but is perhaps useless without any data. Data that typically is stored in enterprise relational databases (such as Microsoft SQL Server, Oracle, DB2) can be explicitly assigned to controls using a series of buttons and event handlers (Load, New, Update, Delete, and so on) by setting values from data values retrieved from databases. Although this is definitely an option, nothing beats the developer productivity benefits that can be achieved through automatic data-bound controls. This is precisely the benefit that the data binding feature of the Windows Forms control library provides. Controls can be bound to DataSet or any other collection objects that implement the IList interface. However, the most commonly used data-binding mechanism is the DataSet, which is typically used to create a read/write link between the input and display of the relational data.

GUI SIMPLIFICATION WITH TAB CONTROLS

Tab controls can be very effective in simplifying a complex form that would have otherwise required the user to scroll. However, an effective Tab control “based application requires careful analysis of how the various forms should be broken up. Sometimes, users expect to fill values in a particular order (for instance based on their experience with paper-based forms), and that order should be respected for the application to be effective.


Data binding can be achieved either by binding individual controls to specific data elements or by binding the entire data set to a special control called the Data Grid. The Data Grid control provides a powerful, updatable mechanism for managing data. As shown in Listing 7.6, data binding in Windows Forms (Figure 7.10) is as simple as assigning the DataSource property to a DataSet. All the rest is taken care of by the core framework itself.

Listing 7.6 Data Binding
 using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace MyCompany {  namespace MyApp  {    class DataBindingForm : Form {    private DataGrid ordersGrid;    private TextBox customerIDTextBox;    private Button goButton;    public DataBindingForm()    {       this.Text = "Data Binding Windows Forms Application";       ordersGrid = new DataGrid();       customerIDTextBox = new TextBox();       goButton = new Button();       goButton.Text = "Get Orders";       customerIDTextBox.Text = "VINET";       goButton.Click += new EventHandler(this.GoButton_Click);       Controls.Add(customerIDTextBox);       Controls.Add(goButton);       Controls.Add(ordersGrid);       customerIDTextBox.Dock = DockStyle.Top;       goButton.Dock = DockStyle.Bottom;       ordersGrid.Dock = DockStyle.Fill;    }    public static void Main()    {       DataBindingForm dbf = new DataBindingForm();       Application.Run(dbf);    }    protected void GoButton_Click(Object sender, EventArgs e)    {       DataSet ordersDataSet = OrdersObject.getOrders(customerIDTextBox.Text);       ordersGrid.DataSource = ordersDataSet;       ordersGrid.DataMember = "Orders";    }    }    class OrdersObject    {    public static DataSet getOrders(String customerID)    {      SqlConnection conn = new SqlConnection(       "Server=localhost;Database=northwind;Trusted_Connection=yes;");      DataSet ordersDataSet = new DataSet();      SqlDataAdapter cmdSelectOrders =         new SqlDataAdapter("Select * from Orders"+            " where CustomerID like '"+customerID+"'", conn);      cmdSelectOrders.Fill(ordersDataSet, "Orders");      return ordersDataSet;    }    }  } } 
Figure 7.10. Data binding form controls.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net