欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#处理datagridview虚拟模式的方法

程序员文章站 2024-02-06 11:27:46
本文实例讲述了c#处理datagridview虚拟模式的方法。分享给大家供大家参考。具体如下: using system; using system.colle...

本文实例讲述了c#处理datagridview虚拟模式的方法。分享给大家供大家参考。具体如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace virtualmode
{
  partial class virtualmodeform : form
  {
   private list<dataobject> m_data = new list<dataobject>();
   private list<bool> m_visited = new list<bool>();
   public virtualmodeform()
   {
     initializecomponent();
     m_grid.cellvalueneeded += oncellvalueneeded;
     m_getvisitedcountbutton.click += ongetvisitedcount;
     initdata();
     initgrid();
   }
   private void initdata()
   {
     for (int i = 0; i < 1000001; i++)
     {
      m_visited.add(false);
      dataobject obj = new dataobject();
      obj.id = i;
      obj.val = 2 * i;
      m_data.add(obj);
     }
   }
   private void initgrid()
   {
     m_grid.virtualmode = true;
     m_grid.readonly = true;
     m_grid.allowusertoaddrows = false;
     m_grid.allowusertodeleterows = false;
     m_grid.columncount = 3;
     m_grid.rows.add();
     m_grid.rows.addcopies(0, 1000000);
     // uncomment the next line and comment out the 
     // the rest of the method to switch to data bound mode
     //m_grid.datasource = m_data;
   }
   private void oncellvalueneeded(object sender,
     datagridviewcellvalueeventargs e)
   {
     m_visited[e.rowindex] = true;
     if (e.columnindex == 0)
     {
      e.value = m_data[e.rowindex].id;
     }
     else if (e.columnindex == 1)
     {
      e.value = m_data[e.rowindex].val;
     }
     else if (e.columnindex == 2)
     {
      random rand = new random();
      e.value = rand.next();
     }
   }
   private void ongetvisitedcount(object sender, eventargs e)
   {
     int count = 0;
     foreach (bool b in m_visited)
     {
      if (b) count++;
     }
     messagebox.show(count.tostring());
   }
    private void virtualmodeform_load(object sender, eventargs e)
    {
    }
  }
  public class dataobject
  {
   private int m_id;
   private int m_val;
   public int val
   {
     get { return m_val; }
     set { m_val = value; }
   }
   public int id
   {
     get { return m_id; }
     set { m_id = value; }
   }
  }
}

希望本文所述对大家的c#程序设计有所帮助。