更新:2007 年 11 月
此示例演示在窗体中使用 DataGrid 控件查看和编辑在 DataGrid 控件中选定的记录,以及向数据库中添加新记录的技术。请注意,由于 .NET Compact Framework 不支持 DataGrid 单元格编辑,因此必须提供用户界面才能编辑 DataGrid 值。此示例使用与 Visual Studio 一起安装的 Northwind 数据库。
| .gif) 说明: | 
|---|
| 如果您使用的是 .NET Compact Framework 2.0,则必须在项目中添加一个对 System.Windows.Forms.DataGrid.dll 的引用,才能使用 DataGrid 控件。 | 
BindingSource 对象提供对数据库中当前选定记录的访问,通过将该对象传递到摘要窗体和编辑窗体的构造函数,所有窗体都可以使用相同的绑定源。除数据绑定控件以外,BindingSource 对象还可以返回当前行的 DataRowView 对象。您可以基于各种目的(如确定某列的当前值)来使用 DataRowView 访问数据。请注意,为便于演示,此示例中的摘要窗体和编辑窗体仅使用了两列数据。
或者,您可以在 DataGrid 控件的智能标记快捷菜单中选择“生成数据窗体”,让 Visual Studio 自动生成摘要窗体和编辑窗体。有关此功能的更多信息,请参见如何:为数据应用程序生成摘要视图和编辑视图(设备)。
此应用程序使用下表中描述的窗体。另外,该表中还列出了其菜单选项。
| 窗体 | 功能 | 菜单选项 | 
|---|---|---|
| 主窗体 (Form1) | 显示 DataGrid 控件。 | 新建 将新记录添加到数据库中,并显示编辑视图窗体。 编辑 显示编辑视图窗体。 | 
| 摘要视图 | 显示当前记录的列值,这些记录已进行了优化以便于查看。 | (无) | 
| 编辑视图 | 显示当前记录的列值,这些记录已进行了优化以便于编辑。 | 完成 接受对话框,更新数据库并显示主窗体。 取消 取消对话框并显示主窗体。 | 
创建项目并设计主窗体
- 在 Visual Studio 中,创建智能设备项目并将目标平台设置为 Windows Mobile 5.0 Pocket PC SDK 或 Windows Mobile 6 Professional 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 控件添加到窗体上。然后按照需要设置其大小和布局属性。 
- 将 DataSource 属性设置为“Products”表。Visual Studio 会将 NorthwindDataSet、ProductsBindingSource 和 ProductsTableAdapter 对象添加到项目中。 
- 通过将一个 DataGridTableStyle 对象添加到 TableStyles 集合中,将 DataGrid 控件的样式设置为显示表中的一列或两列。在“属性”窗格中单击“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)。为“新建”和“编辑”的 Click 事件添加以下代码。 - ' Add new record. Private Sub MenuItem1_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MenuItem1.Click ProductsBindingSource.AllowNew = True ProductsBindingSource.AddNew() ' Pass the binding source to the form. Dim EditViewDialog As New EditView(ProductsBindingSource) If EditViewDialog.ShowDialog() <> DialogResult.OK Then ProductsBindingSource.CancelEdit() Else ProductsBindingSource.EndEdit() Me.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 MenuItem2.Click ' Pass the binding source to the form. Dim EditViewDialog As New EditView(ProductsBindingSource) If EditViewDialog.ShowDialog() <> DialogResult.OK Then ProductsBindingSource.CancelEdit() Else ProductsBindingSource.EndEdit() Me.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); } }
- 在主窗体中,为在 Pocket PC 上按操作键时发生的 DataGrid 控件的 KeyDown 事件添加代码。此操作将显示“摘要视图”窗体。 - ' Action button pressed. 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 New SummaryView(ProductsBindingSource) SummaryViewDialog.ShowDialog() End If End Sub- // Action button pressed. 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”标签。 - 'Dim CurrentBindingSource As BindingSource 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. ' 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; } }
创建编辑视图
- 在项目中添加对 Microsoft.WindowsCE.Forms 命名空间的引用。 
- 从“工具箱”中将一个 InputPanel 组件拖到“编辑视图”窗体上。只需要一个实例,即可为用户提供在文本框中输入文本的软输入面板 (SIP)。 
- 将下列控件添加到窗体中: - 用于“Product Name”文本框的 Label 控件。 
- 用于“Product Name”列的 TextBox 控件。 
- 用于“Discontinued”列的 CheckBox 控件。将其 ThreeState 属性设置为 true。 
 
- 若要设置数据绑定,请在 InitializeComponent 调用之后将以下代码添加到窗体的构造函数中。此代码可用于添加新记录或编辑现有记录。如果添加一条新记录,DataRowView 对象将确定该“Discontinued”列是否包含空值,并将绑定的 NullValue 属性设置为 CheckState 属性的 Indeterminate 值。 - Public Sub New(ByVal bsource As BindingSource) CurrentBindingSource = bsource InitializeComponent() ' Add the bindings. ProductNameTextBox.DataBindings.Add("Text",_ CurrentBindingSource, "Product Name") Dim drView As DataRowView drView = CurrentBindingSource.Current If IsDBNull(drView("Discontinued")) Then DiscontinuedCheckBox.DataBindings.Add("CheckState",_ CurrentBindingSource, "Discontinued", True,_ DataSourceUpdateMode.OnValidation, _ CheckState.Indeterminate) Else DiscontinuedCheckBox.DataBindings.Add("Checked",_ CurrentBindingSource, "Discontinued") End If End Sub- public EditView(BindingSource bsource) { CurrentBindingSource = bsource; InitializeComponent(); CurrentBindingSource = bsource; InitializeComponent(); // Add the bindings. productNameTextBox.DataBindings.Add("Text", CurrentBindingSource, "Product Name"); DataRowView drView; drView = (DataRowView) CurrentBindingSource.Current; if (drView("Discontinued") == null) { DiscontinuedCheckBox.DataBindings.Add("CheckState", CurrentBindingSource, "Discontinued", true, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate); } else { DiscontinuedCheckBox.DataBindings.Add("Checked", CurrentBindingSource, "Discontinued"); } }
- 添加一个标题为“完成”的 MenuItem 对象,以使用更改更新数据库并返回到主窗体。 - ' Done 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 // Done private void menuItem1_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); }
- 在“完成”所在的同一层上添加一个标题为“取消”的 MenuItem 对象,以丢弃更改并返回主窗体。 - ' Cancel 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- // Cancel private void menuItem1_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); }
编译代码
此示例需要引用下面的命名空间: