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