c#处理datagridview虚拟模式

来源:互联网 发布:网络教育考研究生 编辑:程序博客网 时间:2024/06/12 19:48
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace VirtualMode
  9. {
  10.    partial class VirtualModeForm : Form
  11.    {
  12.       private List<DataObject> m_Data = new List<DataObject>();
  13.       private List<bool> m_Visited = new List<bool>();
  14.       public VirtualModeForm()
  15.       {
  16.          InitializeComponent();
  17.          m_Grid.CellValueNeeded += OnCellValueNeeded;
  18.          m_GetVisitedCountButton.Click += OnGetVisitedCount;
  19.          InitData();
  20.          InitGrid();
  21.       }
  22.       private void InitData()
  23.       {
  24.          for (int i = 0; i < 1000001; i++)
  25.          {
  26.             m_Visited.Add(false);
  27.             DataObject obj = new DataObject();
  28.             obj.Id = i;
  29.             obj.Val = 2 * i;
  30.             m_Data.Add(obj);
  31.          }
  32.       }
  33.       private void InitGrid()
  34.       {
  35.          m_Grid.VirtualMode = true;
  36.          m_Grid.ReadOnly = true;
  37.          m_Grid.AllowUserToAddRows = false;
  38.          m_Grid.AllowUserToDeleteRows = false;
  39.          m_Grid.ColumnCount = 3;
  40.          m_Grid.Rows.Add();
  41.          m_Grid.Rows.AddCopies(0, 1000000);
  42.          // Uncomment the next line and comment out the 
  43.          // the rest of the method to switch to data bound mode
  44.          //m_Grid.DataSource = m_Data;
  45.       }
  46.       private void OnCellValueNeeded(object sender,
  47.          DataGridViewCellValueEventArgs e)
  48.       {
  49.          m_Visited[e.RowIndex] = true;
  50.          if (e.ColumnIndex == 0)
  51.          {
  52.             e.Value = m_Data[e.RowIndex].Id;
  53.          }
  54.          else if (e.ColumnIndex == 1)
  55.          {
  56.             e.Value = m_Data[e.RowIndex].Val;
  57.          }
  58.          else if (e.ColumnIndex == 2)
  59.          {
  60.             Random rand = new Random();
  61.             e.Value = rand.Next();
  62.          }
  63.       }
  64.       private void OnGetVisitedCount(object sender, EventArgs e)
  65.       {
  66.          int count = 0;
  67.          foreach (bool b in m_Visited)
  68.          {
  69.             if (b) count++;
  70.          }
  71.          MessageBox.Show(count.ToString());
  72.       }
  73.        private void VirtualModeForm_Load(object sender, EventArgs e)
  74.        {
  75.        }
  76.    }
  77.    public class DataObject
  78.    {
  79.       private int m_Id;
  80.       private int m_Val;
  81.       public int Val
  82.       {
  83.          get { return m_Val; }
  84.          set { m_Val = value; }
  85.       }
  86.       public int Id
  87.       {
  88.          get { return m_Id; }
  89.          set { m_Id = value; }
  90.       }
  91.    }
  92. }