PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/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
19Imports System.Data.SqlClient
20
21Namespace VBWinFormDataGridView.JustInTimeDataLoading
22    Public Class MainForm
23        Private memoryCache As Cache
24
25        ' Specify a connection string. Replace the given value with a
26        ' valid connection string for a Northwind SQL Server sample
27        ' database accessible to your system.
28        Private connectionString As String = "Initial Catalog=NorthWind;Data Source=localhost;" + _
29                       "Integrated Security=SSPI;Persist Security Info=False"
30
31        Private table As String = "Orders"
32
33        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
34            ' Enable VirtualMode on the DataGridView
35            Me.dataGridView1.VirtualMode = True
36            ' Handle the CellValueNeeded event to retrieve the requested cell value
37            ' from the data store or the Customer object currently in edit.
38            ' This event occurs whenever the DataGridView control needs to paint a cell.
39
40            ' Create a DataRetriever and use it to create a Cache object
41            ' and to initialize the DataGridView columns and rows.
42            Try
43                Dim retriever As DataRetriever = New DataRetriever(connectionString, table)
44                memoryCache = New Cache(retriever, 16)
45                For Each column As DataColumn In retriever.Columns
46                    dataGridView1.Columns.Add(column.ColumnName, column.ColumnName)
47                Next
48                Me.dataGridView1.RowCount = retriever.RowCount
49            Catch ex As Exception
50                MessageBox.Show("Connection could not be established. " + _
51                    "Verify that the connection string is valid.")
52                Application.Exit()
53            End Try
54        End Sub
55
56        Private Sub dataGridView1_CellValueNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles dataGridView1.CellValueNeeded
57            e.Value = memoryCache.RetrieveElement(e.RowIndex, e.ColumnIndex)
58        End Sub
59    End Class
60End Namespace