C#处理datagridview虚拟模式的方法
本文实例讲述了C#处理datagridview虚拟模式的方法。分享给大家供大家参考。具体如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; namespaceVirtualMode { partialclassVirtualModeForm:Form { privateList<DataObject>m_Data=newList<DataObject>(); privateList<bool>m_Visited=newList<bool>(); publicVirtualModeForm() { InitializeComponent(); m_Grid.CellValueNeeded+=OnCellValueNeeded; m_GetVisitedCountButton.Click+=OnGetVisitedCount; InitData(); InitGrid(); } privatevoidInitData() { for(inti=0;i<1000001;i++) { m_Visited.Add(false); DataObjectobj=newDataObject(); obj.Id=i; obj.Val=2*i; m_Data.Add(obj); } } privatevoidInitGrid() { 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); //Uncommentthenextlineandcommentoutthe //therestofthemethodtoswitchtodataboundmode //m_Grid.DataSource=m_Data; } privatevoidOnCellValueNeeded(objectsender, DataGridViewCellValueEventArgse) { m_Visited[e.RowIndex]=true; if(e.ColumnIndex==0) { e.Value=m_Data[e.RowIndex].Id; } elseif(e.ColumnIndex==1) { e.Value=m_Data[e.RowIndex].Val; } elseif(e.ColumnIndex==2) { Randomrand=newRandom(); e.Value=rand.Next(); } } privatevoidOnGetVisitedCount(objectsender,EventArgse) { intcount=0; foreach(boolbinm_Visited) { if(b)count++; } MessageBox.Show(count.ToString()); } privatevoidVirtualModeForm_Load(objectsender,EventArgse) { } } publicclassDataObject { privateintm_Id; privateintm_Val; publicintVal { get{returnm_Val;} set{m_Val=value;} } publicintId { get{returnm_Id;} set{m_Id=value;} } } }
希望本文所述对大家的C#程序设计有所帮助。