Creating the Database
You can create your compact database using either Visual Studio or SQL Server. For this example, you will create the tables you need using the built-in functionality of Visual Studio:
- Open your Server Explorer.
- Right-click the Data Connections item.
- Choose "Add Connection."
- Fill in the fields in the Add Connection dialog box (see Figure 3).
- Click the Create button.
- Fill in the fields in the "Create New SQL Server Compact Database" dialog box (see Figure 4) and click OK.
- Test your connection, and then click OK.
|Figure 3. Creating a New Data Connection: Using the Add Connection dialog, specify the data source and connection properties.||
|Figure 4. Creating a New SQL Server Compact Database: Use the dialog shown in this figure to create a new SQL Server compact database.||
With the database created, you can start creating the tables you need for the application by right-clicking the new data connection and choosing "Create table." For the mobile orders application, I created these tables: customer
, and manufacturer
With the database structure in place, you can create strongly typed datasets by dragging each table from the server explorer to the dataset designer. The designer will create all the necessary code for you when you close it.
Windows Forms vs. Mobile Forms
Much of the mobile forms development is the same as Windows Forms; however, there are some distinct differences. These differences include:
- Data binding
- Data grid/custom controls
- Data entry
With Windows Forms, data binding is the accepted best practice; with mobile forms, manual binding is sometimes better. Manual binding is faster than the built-in data binding. Because you are typically dealing with only a handful of controls on any given form, the effort necessary to bind the controls to the data is more trouble than it is worth. However, standard data binding makes sense when you need:
- Master/detail binding
- Currency Manager/BindingSource
Here's a sample of how the mobile orders project uses manual binding to populate the controls:
private void FillOrderHeaderControls(
this.txtCustomer.Text = dr.CustomerName;
this.dtgOrderDate.Value = dr.OrderDate;
private void FillOrderItemControls(
this.txtManPartNo = _dr.ManPartNo;
this.txtQuantity.Text = (string)dr.quantity;
this.txtPrice.Text = _dr.price;
To gather control values to fill a row:
private void FillOrderHeaderRow()
_drOrder = _dtOrder.NewOrderRow();
_drOrder.fkCustomer = _cBiz.GetCustomerKey(
_drOrder.OrderDate = this.dtgOrderDate.Value;
private void FillOrderItemsRow()
_drItem = _dtItem.NewOrderItemRow();
_drItem.ManPartNo = _iBiz.GetManPartNo(value);
_drItem.fkOrder = 0;
_drItem.fkInventory = this.GetInventoryKey
_drItem.Description = this.cboInventory.Text.Trim();
_drItem.quantity = (int)this.txtQuantity.Text;
_drItem.price = (decimal)this.txtPrice.Text
One common question on the Compact Framework forum deals with adding custom controls to grid columns. Out of the box, the DataGrid control does not support custom columns without some extra work—unlike the regular framework DataGrid control.
With the release of .NET CF 2.0 SP1, the team exposed the DataGrid column's Paint
method, which opens the opportunity for custom control creation. Due to the number of requests for help with this feature, they created a downloadable package
that includes a number of useful controls, including the ComboBox, the CheckBbox, and a DateTimePicker. After you reference that library in your project, you can include custom columns with ease:
DataGridCustomTextBoxColumn colOrdNo = new
colOrdNo.Owner = this.grdSearch;
colOrdNo.HeaderText = "No";
colOrdNo.MappingName = "OrderNo";
colOrdNo.Alignment = HorizontalAlignment.Left;
DataGridCustomCheckBoxColumn colTaxable = new
colTaxable.Owner = this.grdSearch;
colTaxable.HeaderText = "Taxable";
colTaxable.MappingName = "Taxable";
colTaxable.Alignment = HorizontalAlignment.Center;
Mobile devices don't support easy data entry into form controls. For the most part, users have to enter text via a built-in hardware mini QWERTY keyboard or using a pop-up software keyboard called a SIP (software input panel). On older smart phones without the hardware keyboard, users must press the number keys on the keypad to enter text. Using any of these options, text entry is difficult, time consuming, and tedious. Therefore, a mobile forms designer must limit the amount of data entry required through the use of judiciously planned controls. Use drop-down lists pre-populated with the most common choices, check boxes, and lists to limit the amount of typing necessary.
Events are another place where mobile development is distinctly different from desktop applications. A typical Windows Form has more than 75 events; a mobile form has 23. A regular button has 60 events; a CF button has 11.
Because of this event disparity, you will need to put more thought into program flow and into creating and designing your mobile forms and projects than your Windows Forms applications.