GridCtrl有没有快速显示批量数据的接口

utakeiteasy 2015-04-27 05:06:28
如题,求各位大神指导
...全文
227 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-04-28
  • 打赏
  • 举报
回复
使用 DataGrid 控件 DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段。可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码。当在设计时设置了 DataGrid 控件的 DataSource 属性后,就会用数据源的记录集来自动填充该控件,以及自动设置该控件的列标头。然后您就可以编辑该网格的列;删除、重新安排、添加列标头、或者调整任意一列的宽度。 在运行时,可以在程序中切换 DataSource 来察看不同的表,或者可以修改当前数据库的查询,以返回一个不同的记录集合。 注意 DataGrid 控件与 Visual Basic 5.0中的 DBGrid 是代码兼容的,除了一个例外:DataGrid 控件不支持 DBGrid 的“解除绑定模式”概念。DBGrid 控件包括在 Visual Basic 98 的 Tools 目录中。 可能的用法 查看和编辑在远程或本地数据库中的数据。 与另一个数据绑定的控件(诸如 DataList 控件)联合使用,使用 DataGrid 控件来显示一个表的记录,这个表通过一个公共字段链接到由第二个数据绑定控件所显示的表。 使用 DataGrid 控件的设计时特性 可以不编写任何代码,只通过使用 DataGrid 控件的设计时特性来创建一个数据库应用程序。下面的说明概要地说明了在实现 DataGrid 控件的典型应用时的一般步骤。完整的循序渐进的指示,请参阅主题“DataGrid 方案1: 使用 DataGrid 控件创建一个简单数据库应用程序”。 要在设计时实现一个 DataGrid 控件 为您希望访问的数据库创建一个 Microsoft 数据链接 (.UDL) 文件。请参阅“创建 Northwind OLE DB 数据链接”主题,以获得一个示例。 在窗体上放置一个 ADO Data 控件,并将其 ConnectionString 属性设置为在第 1 步中所创建的OLE DB 数据源。 在这个 Ado Data 控件的 RecordSource 属性中输入一条将返回一个记录集的 SQL 语句。例如, Select * From MyTableName Where CustID = 12 在窗体上放置一个 DataGrid 控件,并将其 DataSource 属性设置为这个 ADO Data 控件。 右键单击该 DataGrid 控件,然后单击“检索字段”。 右键单击该 DataGrid 控件,然后单击“编辑”。 重新设置该网格的大小、删除、或添加网格的列。 右键单击该 DataGrid 控件,然后单击“属性”。 使用“属性页”对话框来设置该控件的适当的属性,将该网格配置为所需的外观和行为。 在运行时更改显示的数据 在创建了一个使用设计时特性的网格后,也可以在运行时动态地更改该网格的数据源。下面介绍实现这一功能的通常方法。 更改 DataSource 的RecordSource 更改所显示的数据的最通常的方法是改变该 DataSource 的查询。例如,如果 DataGrid 控件使用一个 ADO Data 控件作为其 DataSource,则重写 RecordSource、刷新该 ADO Data 控件、都将改变所显示的数据。 ' ADO Data 控件连接的是 Northwind 数据库的 ' Products 表。新查询查找所有 ' SupplierID = 12 的记录。 Dim strQuery As String strQuery = "SELECT * FROM Suppliers WHERE SupplierID = 12" Adodc1.RecordSource = strQuery Adodc1.Refresh 更改 DataSource 在运行时,可以将 DataSource 属性重新设置为一个不同的数据源。例如,您可能具有若干个 ADO Data 控件,每个控件连接不同的数据库,或设置为不同的 RecordSource 属性。可以简单地将 DataSource 从一个 ADO Data 控件重新设置为另一个 ADO Data 控件: ' 将 DataSource 重新设置为一个连接到 Pubs 数据库的、 ' 使用 Authors 表的 ADO Data 控件。 Set DataGrid1.DataSource = adoPubsAuthors 重新绑定 DataSource 当将 DataGrid 控件用于一个远程数据库,诸如 SQLServer 时,可以改变表的结构。例如,可以给这个表添加一个字段。在这种情形时,可以调用 Rebind 方法根据新的结构来重新创建该网格。注意,如果已经在设计时改变了这个列的布局,DataGrid 控件将会试图重新创建当前的布局,包括任何空的列。不过,通过首先调用 ClearFields 方法,可以强制该网格重新设置所有的列。 从 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 属性。 下一步 要阅读关于使用该控件创建简单的应用程序的一个循序渐进的过程,请参阅“使用 DataGrid 控件创建简单的数据库应用程序”,或“创建一个连接 DataList 控件的 DataGrid”。 要了解关于 Split 对象以及如何对其编程的详细信息,请参阅“操作 DataGrid 视图”。
utakeiteasy 2015-04-28
  • 打赏
  • 举报
回复
@zhao4zhong1
GridCtrl是以表格的形式显示数据,但是除了对单元格依次赋值之外,有别的更好用快捷的方法来显示数据么?
赵4老师 2015-04-27
  • 打赏
  • 举报
回复
Grid Control The Grid control displays and operates on tabular data. It allows complete flexibility to sort, merge, and format tables containing strings. Syntax Grid The properties, methods and events of the Grid control are described in the following table. Properties Methods Events CellAlignment AddItem Compare CellBackColor, CellForeColor Clear (Grid) EnterCell CellFontBold RemoveItem LeaveCell CellFontItalic RowColChange CellFontName Scroll (Grid) CellFontSize SelChange CellFontStrikeThrough CellFontUnderline CellHeight, CellLeft, CellTop, CellWidth CellTextStyle Clip Col ColAlignment ColData CollsVisible ColPos ColPosition, RowPosition Cols, Rows ColSel, RowSel ColWidth FillStyle (Grid) FocusRect ForeColor, ForeColorSel GridColor GridLines GridLineWidth HighLight LeftCol Redraw RowHeight RowHeightMin RowIsVisible RowPos ScrollBars (Grid) ScrollTrack SelectionMode Sort Text (Grid) TextArray TextMatrix TextStyle TopRow Version WordWrap (Grid) Remarks The Grid control supports the following features: Automatic regrouping of data during column adjustment. Ability of each cell to contain text. Changing of current cell text in code or at run time. Word-wrap capability for text within cells. The Grid control can be used in the following scenarios: To implement a "sorting" data display that groups information by category, allowing the user to modify the order in which information is presented. To create a spreadsheet with in-cell editing. The Row and Col properties specify the current cell in a Grid. You can specify the current cell in code, or the user can change it at run time using the mouse or the arrow keys. The Text property references the contents of the current cell. If a cell's text is too long to be displayed in the cell, and the WordWrap property is set to True, the text wraps to the next line within the same cell. To display the wrapped text, you may need to increase the cell's column width (ColWidth property) or row height (RowHeight property). Use the Cols and Rows properties to determine the number of columns and rows in a Grid control.

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧