DataGridViewCellValueEventArgs.ColumnIndex Property

Gets a value indicating the column index of the cell that the event occurs for.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int ColumnIndex { get; }

Property Value

Type: System.Int32
The index of the column containing the cell that the event occurs for.

Use this offset with the Columns property to reference the cell that the event occurs for.

The following code example demonstrates how to use the ColumnIndex property to retrieve cell values from the data store. This example is part of a larger example provided in How to: Implement Virtual Mode in the Windows Forms DataGridView Control.

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. 
    if (e.RowIndex == rowInEdit)
        customerTmp = this.customerInEdit;
        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 "Company Name":
            e.Value = customerTmp.CompanyName;

        case "Contact Name":
            e.Value = customerTmp.ContactName;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft