Question : how to implement virtual mode in the DataGridView control?

hi,

the following code is taken from:

http://msdn.microsoft.com/en-us/library/15a31akc%28v=VS.100%29.aspx

my question is which line indicate that the DataGridView object "dataGridView1" is connected to the data store "customers". As new data is added to "customers", it's also shown in dataGridView1. How's these two connected together?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
public partial class Form1 : Form
    {
        private DataGridView dataGridView1 = new DataGridView();

        // Declare an ArrayList to serve as the data store. 
        private System.Collections.ArrayList customers =
            new System.Collections.ArrayList();

        // Declare a Customer object to store data for a row being edited.
        private Customer customerInEdit;

        // Declare a variable to store the index of a row being edited. 
        // A value of -1 indicates that there is no row currently in edit. 
        private int rowInEdit = -1;

        // Declare a variable to indicate the commit scope. 
        // Set this value to false to use cell-level commit scope. 
        private bool rowScopeCommit = true;

        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.Dock = DockStyle.Fill;
            this.Controls.Add(this.dataGridView1);
            this.Load += new EventHandler(Form1_Load);
            this.Text = "virtual-mode demo";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Enable virtual mode.
            this.dataGridView1.VirtualMode = true;

            // Connect the virtual-mode events to event handlers. 
            this.dataGridView1.CellValueNeeded += new
                DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
            this.dataGridView1.CellValuePushed += new
                DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);
            this.dataGridView1.NewRowNeeded += new
                DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);
            this.dataGridView1.RowValidated += new
                DataGridViewCellEventHandler(dataGridView1_RowValidated);
            this.dataGridView1.RowDirtyStateNeeded += new
                QuestionEventHandler(dataGridView1_RowDirtyStateNeeded);
            this.dataGridView1.CancelRowEdit += new
                QuestionEventHandler(dataGridView1_CancelRowEdit);
            this.dataGridView1.UserDeletingRow += new
                DataGridViewRowCancelEventHandler(dataGridView1_UserDeletingRow);

            // Add columns to the DataGridView.
            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(companyNameColumn);
            this.dataGridView1.Columns.Add(contactNameColumn);
            this.dataGridView1.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;

            // Add some sample entries to the data store. 
            this.customers.Add(new Customer(
                "Bon app'", "Laurence Lebihan"));
            this.customers.Add(new Customer(
                "Bottom-Dollar Markets", "Elizabeth Lincoln"));
            this.customers.Add(new Customer(
                "B's Beverages", "Victoria Ashworth"));

            // Set the row count, including the row for new records.
            this.dataGridView1.RowCount = 4;
        }
......

Answer : how to implement virtual mode in the DataGridView control?

Ohk Sorry for the above answer, I didnt see the link you gave.

The handler dataGridView1_CellValueNeeded does it all. This function is responsible for populating data in the customer list into the datagrid. This function is called whenever there is any change on the grid even if it is redrawn, resized or anything.

dataGridView1_CellValuePushed is called whenever you insert any value in the grid. It adds or updates the Customer object and when you have entered values in both the columns, dataGridView1_RowValidated is called and if the data entered is valid it adds the customer object to the customer arraylist.

Thus making it all in sync.

Thx!
Swaps....
Random Solutions  
 
programming4us programming4us