从 DataGrid 返回值 在 DataGrid 被连接到一个数据库后,可能想要监视用户单击了哪一个单元。您可以使用 RowColChange 事件 — 而不是 Click 事件 — 如下所示,:
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer) ' 显示用户所单击的单元的文字、行和列的信息。 Debug.Print DataGrid1.Text; DataGrid1.Row; DataGrid1.Col End Sub
使用 CellText 和 CellValue 方法 当一个列使用 NumberFormat 属性设置格式后,CellText 和 CellValue 属性是很有用的。NumberFormat 属性不必更改实际的数据的格式就可以更改任何包含数字的列的格式。例如,给定一个网格,其中包含一个名为 ProductID 的、包含整数的列。下面的代码将使 DataGrid 以"P-0000" 的格式来显示数据。换句话说,尽管在 ProductID 字段中所包含的实际数值为 "3",但该网格所显示的值将是 "P-0003"。
Private Sub Form_Load() DataGrid1.Columns("ProductID").NumberFormat = "P-0000" End Sub
要返回数据库中所包含的实际值,应使用 CellValue 方法,如下所示:
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer) Debug.Print _ DataGrid1.Columns("ProductID").CellValue(DataGrid1.Bookmark) End Sub
注意 上面所用的 CellValue 和下面所用的 CellText 值,都需要将 bookmark 属性作为一个参数,功能才正确。
相反地,如果要返回该字段的格式化的值,应使用 CellText 方法:
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer) Debug.Print _ DataGrid1.Columns("ProductID").CellText(DataGrid1.Bookmark) End Sub
注意 上面的 CellText 方法等价于使用 DataGrid 控件的 Texr 属性。
上一页 [1] [2]
|