更新:2007 年 11 月
您可以创建类似于 Smartphone“联系人”程序的 Smartphone 应用程序。
| .gif) 说明: | 
|---|
| 如果您使用的是 3.5 之前的 .NET Compact Framework 版本,则必须在项目中添加一个对 System.Windows.Forms.DataGrid.dll 的引用才能使用它。 | 
此示例演示了一个主窗体,其中的 DataGrid 控件包含一个来自 Northwind 数据库的产品名称列表。Northwind 数据库是随 Visual Studio 一起安装的。此外,它还包含用于显示当前记录的摘要视图窗体,以及用于编辑数据和添加新记录的编辑视图窗体。BindingSource 对象提供对数据库中当前选定记录的访问。除数据绑定控件以外,BindingSource 对象还可以返回当前行的 DataRowView 对象。您可以基于各种目的(如确定某列的当前值)来使用 DataRowView 访问数据。
或者,您可以在 DataGrid 控件的智能标记快捷菜单中选择“生成数据窗体”,让 Visual Studio 自动生成摘要窗体和编辑窗体。请注意,为便于演示,此示例中的摘要窗体和编辑窗体仅使用了两列数据。
此应用程序使用下表中描述的窗体。另外,表中还列出了 Smartphone 左右软键的菜单选项。
| 窗体 | 功能 | 左软键 | 右软键 | 
|---|---|---|---|
| 主窗体 (Form1) | 采用 Smartphone“联系人”列表样式,在 DataGrid 控件中显示表中的一列。 按“操作”键,或通过仿真程序按键盘上的 Enter,显示摘要视图窗体。 | 新建 将新记录添加到数据库中,并显示编辑视图窗体。 | 编辑 显示编辑视图窗体。 | 
| 摘要视图 | 显示当前记录的列值,这些记录已进行了优化以便于查看。 | 完成 返回到主窗体。 | (无) | 
| 编辑视图 | 显示当前记录的列值,这些记录已进行了优化以便于编辑。 | 完成 接受对话框,更新数据库并显示主窗体。 | 取消 取消对话框并显示主窗体。 | 
创建项目并设计主窗体
- 在 Visual Studio 中,创建智能设备项目并将目标平台设置为 Windows Mobile 5.0 for Smartphone SDK 或 Windows Mobile 6 Standard SDK。 
- 在“数据”菜单上单击“添加新数据源”。 
- 在“数据源配置向导”中,使用 Microsoft SQL Server Compact Edition(用于 SQL Server CE 的 .NET Framework 数据提供程序)连接到“Northwind”数据库。“Northwind”数据库(即 Northwind.sdf)安装在 \Program Files\Microsoft SQL Server Compact Edition\v3.5\Samples 文件夹中。 .gif) 说明: 说明:- 在 Windows Vista 上,您必须以管理员身份运行 Visual Studio 才能访问 Northwind 数据库。有关添加数据库的更多信息,请参见如何:向设备项目添加数据库。 
- 在该向导的“选择数据库对象”页中,选择“Products”表及其所有列。 
- 从“工具箱”中将 DataGrid 控件添加到窗体上。 
- 为使 DataGrid 控件外观与 Smartphone“联系人”列表类似,请按照下表所示设置其属性。 - DataGrid 属性 - 设置为 - False - False - Point 结构,其中 x 为 -2,y 为 -2。 - Size 结构,其中宽度为 184,高度为 190。 
- 将 DataSource 属性设置为“Orders”表。Visual Studio 会将 NorthwindDataSet、NorthwindDataSet 和 NorthwindDataSet 对象添加到项目中。 
- 在“属性”窗格中单击“TableStyles”属性。此操作会显示“DataGridTableStyle 集合编辑器”对话框。然后执行下列操作: - 将 DataGridTableStyle 对象添加到 TableStyles 集合中。 
- 为“MappingName”属性指定“Products”。 
- 单击“GridColumnStyle”属性。此操作会显示“DataGridColumnStyle 集合编辑器”对话框。 
- 将 DataGridTextBoxColumn 对象添加到 GridColumnStyles 集合中。 
- 单击“MappingName”属性并选择“Product Name”。 
- 设置所需的“页眉文本”和“宽度”。 
- 对其他列重复以上操作。 
- 关闭该对话框。 
 
- 在项目中添加两个窗体,分别用于摘要视图和编辑视图,并将它们各自命名为“摘要视图”和“编辑视图”。 
- 向“摘要视图”和“编辑视图”窗体的构造函数添加一个参数,以接受 BindingSource 对象。声明一个全局变量 CurrentBindingSouce,此变量在这些窗体中将设为传入构造函数的 BindingSource 对象。请注意,应在调用 InitializeComponent 方法之前进行设置。 - Visual Basic 开发人员应通过从代码窗格右上角的“方法名”列表添加“New”方法,为窗体添加一个“Sub New”。 - Dim CurrentBindingSource As BindingSource Public Sub New(ByVal bsource As BindingSource) CurrentBindingSource = bsource InitializeComponent() End Sub- private BindingSource CurrentBindingSource; public SummaryView(BindingSource bsource) { CurrentBindingSource = bsource; InitializeComponent(); }
- 在主窗体中,添加两个 MenuItem 对象,分别命名为“新建”(MenuItem1) 和“编辑” (MenuItem2)。这些菜单对应 Smartphone 左右软键。为“新建”和“编辑”的 Click 事件添加以下代码。 - ' Add new record. Private Sub MenuItem1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem1.Click ProductsBindingSource.AddNew() Dim EditViewDialog As New EditView(ProductsBindingSource) If EditViewDialog.ShowDialog() <> DialogResult.OK Then ProductsBindingSource.CancelEdit() Else ProductsBindingSource.EndEdit() ProductsTableAdapter.Update(Me.NorthwindDataSet) End If End Sub ' Edit record. Private Sub MenuItem2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem1.Click Dim EditViewDialog As New EditView(ProductsBindingSource) If EditViewDialog.ShowDialog() <> DialogResult.OK Then ProductsBindingSource.CancelEdit() Else ProductsBindingSource.EndEdit() ProductsTableAdapter.Update(Me.NorthwindDataSet) End If End Sub- // Add new record. private void menuItem1_Click(object sender, EventArgs e) { productsBindingSource.AllowNew = true; productsBindingSource.AddNew(); EditView EditViewDialog = new EditView(productsBindingSource); if (EditViewDialog.ShowDialog() != DialogResult.OK) { productsBindingSource.CancelEdit(); } else { ProductsBindingSource.EndEdit(); this.productsTableAdapter.Update(this.northwindDataSet); } } // Edit record (Edit). private void menuItem2_Click(object sender, EventArgs e) { EditView EditViewDialog = new EditView(productsBindingSource); if (EditViewDialog.ShowDialog() != DialogResult.OK) { productsBindingSource.CancelEdit(); } else { productsBindingSource.EndEdit(); this.productsTableAdapter.Update(this.northwindDataSet); } }
- 在主窗体中,为在 Smartphone 上按操作键时发生的 KeyDown 事件添加代码。此操作将显示“摘要视图”窗体。 - Private Sub DataGrid1_KeyDown(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles DataGrid1.KeyDown If (e.KeyCode = Keys.Enter) Then Dim SummaryViewDialog As SummaryView = New SummaryView(ProductsBindingSource) Cursor.Current = Cursors.Default SummaryView.ShowDialog() End If End Sub- private void dataGrid1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { SummaryView SummaryViewDialog = new SummaryView(productsBindingSource); SummaryViewDialog.ShowDialog(); } }
创建摘要视图
- 将下列控件添加到窗体中: 
- 将以下代码添加到“摘要视图”窗体的构造函数中,以设置数据绑定。声明一个名为 CurrentBindingSource 的窗体变量,并将其设为在窗体构造函数中传递的 BindingSource 实例。DataRowView 对象确定了如果“Discontinued”列为 true,则显示“Discontinued”标签。 - Public Sub New(ByVal bsource As BindingSource) CurrentBindingSource = bsource ' This call is required by the Windows Forms Designer. InitializeComponent() ' Bind the label that shows the product name. ProductNameLabelVal.DataBindings.Add("Text", _ CurrentBindingSource, "Product Name") ' Show the Discontinued label if ' that value is true in the database. Dim drView As DataRowView drView = CurrentBindingSource.Current If drView.Item("Discontinued") = True Then DiscontinuedLabel.Visible = True Else DiscontinuedLabel.Visible = False End If End Sub- private BindingSource CurrentBindingSource; public SummaryView(BindingSource bsource) { CurrentBindingSource = bsource; InitializeComponent(); // Bind the label that shows the product name. ProductNameLabelVal.DataBindings.Add("Text", CurrentBindingSource, "Product Name"); // Show the Discontinued label if // that value is true in the database. DataRowView drView; drView = (DataRowView) CurrentBindingSource.Current; if (drView["Discontinued"] == true) { DiscontinuedLabel.Visible = true; } else { DiscontinuedLabel.Visible = false; } }
- 为左软键添加一个标题为“完成”的 MenuItem 对象,以关闭该窗体并返回到主窗体。 - Private Sub MenuItem1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem1.Click Me.Close End Sub- private void MenuItem1_Click(object sender, System.EventArgs e) { this.Close(); }
创建编辑视图
- 在项目中添加对 Microsoft.WindowsCE.Forms 命名空间的引用。这对于在文本框控件上设置 Smartphone 的 InputMode 设置是必需的。 
- 将下列控件添加到窗体中: 
- 若要设置数据绑定,请在 InitializeComponent 调用之后将以下代码添加到窗体的构造函数中。此代码可用于添加新记录或编辑现有记录。如果正在添加新记录,DataRowView 对象将会确定“Discontinued”列是否为空值。如果值为空,复选框便会设置为 false。 - Public Sub New(ByVal bsource As BindingSource) CurrentBindingSource = bsource ' This call is required by the Windows Forms Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. ' Set the Smartphone input mode. InputModeEditor.SetInputMode(ProductNameTextBox,_ InputMode.AlphaT9) ProductNameTextBox.DataBindings.Add("Text",_ CurrentBindingSource, "Product Name") ' Determine the Discontinued value. ' If null, change to False. Dim drView As DataRowView drView = CurrentBindingSource.Current ' Set the bindings. If IsDBNull(drView("Discontinued")) Then DiscontinuedCheckBox.DataBindings.Add("CheckState",_ CurrentBindingSource, "Discontinued", True,_ DataSourceUpdateMode.OnValidation, False, "") Else DiscontinuedCheckBox.DataBindings.Add("Checked",_ CurrentBindingSource, "Discontinued") End If End Sub- public EditView(BindingSource bsource) { CurrentBindingSource = bsource; InitializeComponent(); // Set the Smartphone input mode. InputModeEditor.SetInputMode(ProductNameTextBox, InputMode.AlphaT9); // Set the bindings. ProductNameTextBox.DataBindings.Add("Text", CurrentBindingSource,"Product Name"); // Determine the Discontinued value. // If null, change to False. DataRowView drView; drView = (DataRowView) CurrentBindingSource.Current; if(drView("Discontinued")== null) { DiscontinuedCheckBox.DataBindings.Add("CheckState", CurrentBindingSource, "Discontinued", true,DataSourceUpdateMode.OnValidation,false,""); } else { DiscontinuedCheckBox.DataBindings.Add("Checked", CurrentBindingSource, "Discontinued"); } }
- 为左软键添加一个标题为“完成”的 MenuItem 对象,以使用所做更改更新数据库并返回到主窗体。 - Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click Me.DialogResult = DialogResult.OK Me.Close() End Sub- Private void MenuItem1_Click(object sender, System.EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); }
- 为右软键添加一个标题为“取消”的 MenuItem 对象,以放弃更改并返回到主窗体。 - Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click Me.DialogResult = DialogResult.Cancel Me.Close() End Sub- Private void MenuItem2_Click(object sender, System.EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); }
编译代码
此示例需要引用下面的命名空间: