PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/JustInTimeDataLoading/MainForm.cs

#
C# | 90 lines | 55 code | 10 blank | 25 comment | 0 complexity | cbe8063dfefbfb858d6ae583a4959bbf MD5 | raw file
 1/********************************* Module Header **********************************\
 2* Module Name:	JustInTimeDataLoading
 3* Project:		CSWinFormDataGridView
 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* History:
15* * 6/10/2009 3:00 PM Zhi-Xin Ye Created
16\**********************************************************************************/
17
18#region Using directives
19using System;
20using System.Collections.Generic;
21using System.ComponentModel;
22using System.Data;
23using System.Drawing;
24using System.Linq;
25using System.Text;
26using System.Windows.Forms;
27using System.Data.SqlClient;
28#endregion
29
30
31namespace CSWinFormDataGridView.JustInTimeDataLoading
32{
33    public partial class MainForm : Form
34    {
35        public MainForm()
36        {
37            InitializeComponent();
38        }
39
40        private Cache memoryCache;
41
42        // Specify a connection string. Replace the given value with a 
43        // valid connection string for a Northwind SQL Server sample
44        // database accessible to your system.
45        private string connectionString =
46            "Initial Catalog=NorthWind;Data Source=localhost;" +
47            "Integrated Security=SSPI;Persist Security Info=False";
48
49        private string table = "Orders";
50
51        private void MainForm_Load(object sender, EventArgs e)
52        {
53            // Enable VirtualMode on the DataGridView
54            this.dataGridView1.VirtualMode = true;
55
56            // Handle the CellValueNeeded event to retrieve the requested cell value 
57            // from the data store or the Customer object currently in edit.
58            // This event occurs whenever the DataGridView control needs to paint a cell. 
59            this.dataGridView1.CellValueNeeded += new
60                DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
61
62            // Create a DataRetriever and use it to create a Cache object
63            // and to initialize the DataGridView columns and rows.
64            try
65            {
66                DataRetriever retriever =
67                    new DataRetriever(connectionString, table);
68                memoryCache = new Cache(retriever, 16);
69                foreach (DataColumn column in retriever.Columns)
70                {
71                    dataGridView1.Columns.Add(
72                        column.ColumnName, column.ColumnName);
73                }
74                this.dataGridView1.RowCount = retriever.RowCount;
75            }
76            catch (SqlException)
77            {
78                MessageBox.Show("Connection could not be established. " +
79                    "Verify that the connection string is valid.");
80                Application.Exit();
81            }
82        }
83
84        private void dataGridView1_CellValueNeeded(object sender,
85            DataGridViewCellValueEventArgs e)
86        {
87            e.Value = memoryCache.RetrieveElement(e.RowIndex, e.ColumnIndex);
88        }
89    }
90}