M
Mou_kolkata
Guest
this is first time i am using DataGridViews 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.
public partial class VirtualModeForm1 : Form
{
BindingList<Customer> _customers = new BindingList<Customer>();
DateTime startRefreshTime;
DateTime endRefreshTime;
const int NUMROWS = 10000;
public VirtualModeForm1()
{
InitializeComponent();
InitializeGrid();
InitializeData();
dataGridView1.Refresh();
}
private void InitializeGrid()
{
// Enable virtual mode.
this.dataGridView1.VirtualMode = true;
// Connect the virtual-mode events to event handlers.
this.dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
// Add columns to the DataGridView.
DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn();
IDColumn.HeaderText = "ID";
IDColumn.Name = "ID";
DataGridViewTextBoxColumn companyNameColumn = new DataGridViewTextBoxColumn();
companyNameColumn.HeaderText = "Company Name";
companyNameColumn.Name = "Company Name";
DataGridViewTextBoxColumn contactNameColumn = new DataGridViewTextBoxColumn();
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;
}
void InitializeData()
{
AddCustomerData();
}
private void AddCustomerData()
{
for (int i = 0; i < NUMROWS; i++)
{
int id = i;
string company = "ABC" + i + " Corp";
string name = "A" + i + "V" + i;
Customer c = new Customer(id,company, name);
this._customers.Add(c);
}
// Set the row count, including the row for new records.
this.dataGridView1.RowCount = NUMROWS;
}
private void 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;
}
}
}
public class Customer : IComparable
{
private String companyNameValue;
private String contactNameValue;
private int id = 0;
public Customer()
{
// Leave fields empty.
}
public Customer(int id,String companyName, String contactName)
{
ID = id;
companyNameValue = companyName;
contactNameValue = contactName;
}
static public int CompareIndex = 0;
public int CompareTo(object obj)
{
// TODO: Add Address.CompareTo implementation
Customer cust1 = this;
Customer cust2 = (Customer)obj;
return cust1.ContactName[CompareIndex].CompareTo(cust2.ContactName[CompareIndex]);
}
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}
public String CompanyName
{
get
{
return companyNameValue;
}
set
{
companyNameValue = value;
}
}
public String ContactName
{
get
{
return contactNameValue;
}
set
{
contactNameValue = value;
}
}
}
Continue reading...
public partial class VirtualModeForm1 : Form
{
BindingList<Customer> _customers = new BindingList<Customer>();
DateTime startRefreshTime;
DateTime endRefreshTime;
const int NUMROWS = 10000;
public VirtualModeForm1()
{
InitializeComponent();
InitializeGrid();
InitializeData();
dataGridView1.Refresh();
}
private void InitializeGrid()
{
// Enable virtual mode.
this.dataGridView1.VirtualMode = true;
// Connect the virtual-mode events to event handlers.
this.dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
// Add columns to the DataGridView.
DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn();
IDColumn.HeaderText = "ID";
IDColumn.Name = "ID";
DataGridViewTextBoxColumn companyNameColumn = new DataGridViewTextBoxColumn();
companyNameColumn.HeaderText = "Company Name";
companyNameColumn.Name = "Company Name";
DataGridViewTextBoxColumn contactNameColumn = new DataGridViewTextBoxColumn();
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;
}
void InitializeData()
{
AddCustomerData();
}
private void AddCustomerData()
{
for (int i = 0; i < NUMROWS; i++)
{
int id = i;
string company = "ABC" + i + " Corp";
string name = "A" + i + "V" + i;
Customer c = new Customer(id,company, name);
this._customers.Add(c);
}
// Set the row count, including the row for new records.
this.dataGridView1.RowCount = NUMROWS;
}
private void 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;
}
}
}
public class Customer : IComparable
{
private String companyNameValue;
private String contactNameValue;
private int id = 0;
public Customer()
{
// Leave fields empty.
}
public Customer(int id,String companyName, String contactName)
{
ID = id;
companyNameValue = companyName;
contactNameValue = contactName;
}
static public int CompareIndex = 0;
public int CompareTo(object obj)
{
// TODO: Add Address.CompareTo implementation
Customer cust1 = this;
Customer cust2 = (Customer)obj;
return cust1.ContactName[CompareIndex].CompareTo(cust2.ContactName[CompareIndex]);
}
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}
public String CompanyName
{
get
{
return companyNameValue;
}
set
{
companyNameValue = value;
}
}
public String ContactName
{
get
{
return contactNameValue;
}
set
{
contactNameValue = value;
}
}
}
Continue reading...