Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to capture DataGridView virtual mode loading data on demand

$
0
0

this is first time i am using DataGridView's virtual property but could not see how it load data on demand. may be i made some mistake in code. my code is working fine and when i scroll down then i saw data is there but i like to feel data load on demand when scroll down. here i am giving my code and tell me what i missed there for which lazy load like feel is not there.

publicpartialclassVirtualModeForm1:Form{BindingList<Customer> _customers =newBindingList<Customer>();DateTime startRefreshTime;DateTime endRefreshTime;constint NUMROWS =10000;publicVirtualModeForm1(){InitializeComponent();InitializeGrid();InitializeData();
            dataGridView1.Refresh();}privatevoidInitializeGrid(){// Enable virtual mode.this.dataGridView1.VirtualMode=true;// Connect the virtual-mode events to event handlers. this.dataGridView1.CellValueNeeded+=newDataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);// Add columns to the DataGridView.DataGridViewTextBoxColumnIDColumn=newDataGridViewTextBoxColumn();IDColumn.HeaderText="ID";IDColumn.Name="ID";DataGridViewTextBoxColumn companyNameColumn =newDataGridViewTextBoxColumn();

            companyNameColumn.HeaderText="Company Name";
            companyNameColumn.Name="Company Name";DataGridViewTextBoxColumn contactNameColumn =newDataGridViewTextBoxColumn();

            contactNameColumn.HeaderText="Contact Name";
            contactNameColumn.Name="Contact Name";this.dataGridView1.Columns.Add(IDColumn);this.dataGridView1.Columns.Add(companyNameColumn);this.dataGridView1.Columns.Add(contactNameColumn);this.dataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.ColumnHeader;// Set the row count, including the row for new records.this.dataGridView1.RowCount= NUMROWS;}voidInitializeData(){AddCustomerData();}privatevoidAddCustomerData(){for(int i =0; i < NUMROWS; i++){int id = i;string company ="ABC"+ i +" Corp";string name ="A"+ i +"V"+ i;Customer c =newCustomer(id,company, name);this._customers.Add(c);}// Set the row count, including the row for new records.this.dataGridView1.RowCount= NUMROWS;}privatevoid dataGridView1_CellValueNeeded(object sender,System.Windows.Forms.DataGridViewCellValueEventArgs e){// If this is the row for new records, no values are needed.if(e.RowIndex==this.dataGridView1.RowCount-1)return;Customer customerTmp =null;// Store a reference to the Customer object for the row being painted.     
            customerTmp =(Customer)this._customers[e.RowIndex];// Set the cell value to paint using the Customer object retrieved.switch(this.dataGridView1.Columns[e.ColumnIndex].Name){case"ID":
                    e.Value= customerTmp.ID;break;case"Company Name":
                    e.Value= customerTmp.CompanyName;break;case"Contact Name":
                    e.Value= customerTmp.ContactName;break;}}}publicclassCustomer:IComparable{privateString companyNameValue;privateString contactNameValue;privateint id =0;publicCustomer(){// Leave fields empty.}publicCustomer(int id,String companyName,String contactName){
            ID = id;
            companyNameValue = companyName;
            contactNameValue = contactName;}staticpublicintCompareIndex=0;publicintCompareTo(object obj){// TODO:  Add Address.CompareTo implementationCustomer cust1 =this;Customer cust2 =(Customer)obj;return cust1.ContactName[CompareIndex].CompareTo(cust2.ContactName[CompareIndex]);}publicint ID{
            get{return id;}set{
                id = value;}}publicStringCompanyName{
            get{return companyNameValue;}set{
                companyNameValue = value;}}publicStringContactName{
            get{return contactNameValue;}set{
                contactNameValue = value;}}}


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>