[引]在Pocket PC上使用 DataGrid

本文引自VS2005的帮助文档 并做了部分可运行的修改

此示例演示在窗体中使用 DataGrid 控件查看和编辑在 DataGrid 控件中选定的记录,
以及向数据库中添加新记录的技术。
请注意,由于 .NET Compact Framework 不支持 DataGrid 单元格编辑,
因此必须提供用户界面 (UI) 才能编辑 DataGrid 值。
此示例使用与 Microsoft Visual Studio 2005 一起安装的 Northwind 数据库。

BindingSource 对象提供对数据库中当前选定记录的访问,
通过将该对象传递到摘要窗体和编辑窗体的构造函数,
所有窗体都可以使用相同的绑定源。
除数据绑定控件以外,BindingSource 对象还可以返回当前行的 DataRowView 对象。
您可以基于各种目的(如确定某列的当前值)来使用 DataRowView 访问数据。
请注意,为便于演示,此示例中的摘要窗体和编辑窗体仅使用了两列数据。

或者,您可以在 DataGrid 控件的智能标记快捷菜单中选择“生成数据窗体”,
让 Visual Studio 2005 自动生成摘要窗体和编辑窗体。

此应用程序使用下表中描述的窗体。另外,该表中还列出了其菜单选项。

窗体         功能                                 菜单选项 
主窗体     显示 DataGrid 控件     新建:将新记录添加到数据库中,并显示编辑视图窗体
(Form1)                                              编辑:显示编辑视图窗体。
 
摘要视图   显示当前记录的列值     返回:取消对话框并显示主窗体
(SummaryView)
 
编辑视图   显示当前记录的列值    完成:接受对话框,更新数据库并显示主窗体
(EditView)                                             取消:取消对话框并显示主窗体

具体步骤如下:
-------------

创建项目并设计主窗体
1.在 Visual Studio 2005 中,创建一个 Pocket PC 设备项目。
2.从“数据”菜单中选择“添加新数据源”。
  通过 Microsoft SQL Server Mobile Edition
  (.NET Framework Data Provider for SQL Server CE),
  使用数据源配置向导连接到“Northwind”数据库。
 “Northwind”数据库 (Northwind.sdf)
  安装在 \Program Files\Microsoft Visual Studio 8\SmartDevices\SDK
                        \SQL Server\Mobile\v3.0 文件夹中。

  在该向导的“选择数据库对象”步骤中,选择“Products”表及其所有列。
3.从“工具箱”中将 DataGrid 控件添加到窗体上。然后按照需要设置其大小和布局属性。
4.将 DataSource 属性设置为“Products”表。
  Visual Studio 2005 会将 NorthwindDataSet、ProductsBindingSource
                       和 ProductsTableAdapter 对象添加到项目中。
5.通过将一个 DataGridTableStyle 对象添加到 TableStyles 集合中,
  将 DataGrid 控件的样式设置为显示表中的一列或两列。
  在“属性”窗格中单击“TableStyles”属性。
  此操作会显示“DataGridTableStyle 集合编辑器”对话框。
  然后执行下列操作:
  将 DataGridTableStyle 对象添加到 TableStyles 集合中。
  为“MappingName”属性指定“Products”。
  单击“ColumnGridStyle”属性。
  此操作会显示“DataGridColumnStyle 集合编辑器”对话框。
  将 DataGridTextBoxColumn 对象添加到 GridColumnStyles 集合中。
  单击“MappingName”属性并选择“Product Name”。
  设置所需的“页眉文本”和“宽度”。

  对其他列重复以上操作。
  关闭该对话框。

6.在项目中添加两个窗体,分别用于摘要视图和编辑视图,
  并将它们各自命名为“SummaryView(摘要视图)”和“EditView(编辑视图)”。
7.向“SummaryView”和“EditView”窗体的构造函数添加一个参数,
  以接受 BindingSource 对象。
  声明一个全局变量 CurrentBindingSouce,
  并在这些窗体中将其设为在构造函数中传递的 BindingSource 对象。
  请注意,应在调用 InitializeComponent 方法之前进行设置。
  示例代码如下:
  Visual Basic  
  Dim CurrentBindingSource As BindingSource
  Public Sub New(ByVal bsource As BindingSource)
      CurrentBindingSource = bsource
      InitializeComponent()
  End Sub

  C#  
  private BindingSource CurrentBindingSource;
  public SummaryView(BindingSource bsource)
  {
      CurrentBindingSource = bsource;
      InitializeComponent();
  }

8.在主窗体(Form1)中,添加两个 MenuItem 对象,
  分别命名为“新建” (MenuItem1) 和“编辑” (MenuItem2)。
  为“新建”和“编辑”的 Click 事件添加以下代码。

  Visual Basic 
  ' 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
 
  C#  
  // 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);
      }
  }
 
9.在主窗体(Form1)中,
  为在 Pocket PC 上按操作键时发生的 DataGrid 控件的 KeyDown 事件添加代码。
  此操作将显示“摘要视图”窗体。

  Visual Basic 
  ' 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

  C# 
  // Action button pressed.
  private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
  {
      if (e.KeyCode == Keys.Enter)
      {
          SummaryView SummaryViewDialog =
            new SummaryView(productsBindingSource);
          SummaryViewDialog.ShowDialog();
      }
  }

10.编辑摘要视图(SummaryView)
将下列控件添加到“摘要视图”窗体中:
用于“Product Name”标题的 Label 控件,如“产品名称:”。
用于“Product Name”值的 Label 控件。
用于“Discontinued”值的 Label 控件,
  该控件仅在“Products”表“Discontinued”列中的值为 true 时才会显示。
  使用红色字体将此标签的标题设为“DISCONTINUED”。

将以下代码添加到“摘要视图”窗体的构造函数中,以设置数据绑定。
声明一个名为 CurrentBindingSource 的窗体变量,
并将其设为在窗体构造函数中传递的 BindingSource 实例。
DataRowView 对象确定了如果“Discontinued”列为 true,则显示“Discontinued”标签。

Visual Basic  复制代码
'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
 
C#  复制代码
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 对象,以使用更改更新数据库并返回到主窗体。

Visual Basic  复制代码
' 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

C#
// Done
private void menuItem1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}
 


11.编辑编辑视图(EditView)
从“工具箱”中将一个 InputPanel 组件拖到“编辑视图”窗体上。
只需要一个实例,即可为用户提供在文本框中输入文本的软输入面板 (SIP)。

将下列控件添加到窗体中:
用于“Product Name”文本框的 Label 控件。
用于“Product Name”列的 TextBox 控件。
用于“Discontinued”列的 CheckBox 控件。将其 ThreeState 属性设置为 true。

若要设置数据绑定,请在窗体构造函数中的 InitializeComponent 调用后添加如下所示的代码。此代码可用于添加新记录或编辑现有记录。如果添加一条新记录,DataRowView 对象将确定该“Discontinued”列是否包含空值,并将绑定的 NullValue 属性设置为 CheckState 属性的 Indeterminate 值。

Visual Basic  复制代码
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
 
C#  复制代码
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 对象,以使用更改更新数据库并返回到主窗体。

Visual Basic  复制代码
' 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

C#
// Done
private void menuItem1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}
 

在“完成”所在的同一层上添加一个标题为“取消”的 MenuItem 对象,以丢弃更改并返回主窗体。

Visual Basic  复制代码
' 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
 
C#  复制代码
// Cancel
private void menuItem2_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
    this.Close();
}

12.保存 生成 并调试

posted on 2007-02-09 19:35  freeliver54  阅读(2146)  评论(0编辑  收藏  举报

导航