PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/VBWinFormDataGridView/JustInTimeDataLoading/MainForm.vb

#
Visual Basic | 60 lines | 27 code | 7 blank | 26 comment | 0 complexity | 79ffef5fe672f745a11808eb7ed82512 MD5 | raw file
  1. '********************************* Module Header **********************************\
  2. ' Module Name: JustInTimeDataLoading
  3. ' Project: VBWinFormDataGridView
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' This sample demonstrates how to use virtual mode in the DataGridView control
  7. ' with a data cache that loads data from a server only when it is needed.
  8. ' This kind of data loading is called "Just-in-time data loading".
  9. '
  10. ' This source is subject to the Microsoft Public License.
  11. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  12. ' All other rights reserved.
  13. '
  14. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  15. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  16. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  17. '**********************************************************************************/
  18. Imports System.Data.SqlClient
  19. Namespace VBWinFormDataGridView.JustInTimeDataLoading
  20. Public Class MainForm
  21. Private memoryCache As Cache
  22. ' Specify a connection string. Replace the given value with a
  23. ' valid connection string for a Northwind SQL Server sample
  24. ' database accessible to your system.
  25. Private connectionString As String = "Initial Catalog=NorthWind;Data Source=localhost;" + _
  26. "Integrated Security=SSPI;Persist Security Info=False"
  27. Private table As String = "Orders"
  28. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  29. ' Enable VirtualMode on the DataGridView
  30. Me.dataGridView1.VirtualMode = True
  31. ' Handle the CellValueNeeded event to retrieve the requested cell value
  32. ' from the data store or the Customer object currently in edit.
  33. ' This event occurs whenever the DataGridView control needs to paint a cell.
  34. ' Create a DataRetriever and use it to create a Cache object
  35. ' and to initialize the DataGridView columns and rows.
  36. Try
  37. Dim retriever As DataRetriever = New DataRetriever(connectionString, table)
  38. memoryCache = New Cache(retriever, 16)
  39. For Each column As DataColumn In retriever.Columns
  40. dataGridView1.Columns.Add(column.ColumnName, column.ColumnName)
  41. Next
  42. Me.dataGridView1.RowCount = retriever.RowCount
  43. Catch ex As Exception
  44. MessageBox.Show("Connection could not be established. " + _
  45. "Verify that the connection string is valid.")
  46. Application.Exit()
  47. End Try
  48. End Sub
  49. Private Sub dataGridView1_CellValueNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles dataGridView1.CellValueNeeded
  50. e.Value = memoryCache.RetrieveElement(e.RowIndex, e.ColumnIndex)
  51. End Sub
  52. End Class
  53. End Namespace