Export (0) Print
Expand All

DataGridViewCellParsingEventArgs.ColumnIndex Property

Gets the column index of the cell data that requires parsing.

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

public int ColumnIndex { get; }

Property Value

Type: System.Int32
The column index of the cell that was changed.

Use the ColumnIndex property to obtain an index into the Columns property of a DataGridView.

The following code example demonstrates using ColumnIndex to discover if the changed cell is in the date column.

// Handling CellParsing allows one to accept user input, then map it to a different 
// internal representation. 
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        if (e != null)
        {
            if (e.Value != null)
            {
                try
                {
                    // Map what the user typed into UTC.
                    e.Value = DateTime.Parse(e.Value.ToString()).ToUniversalTime();
                    // Set the ParsingApplied property to  
                    // Show the event is handled.
                    e.ParsingApplied = true;

                }
                catch (FormatException)
                {
                    // Set to false in case another CellParsing handler 
                    // wants to try to parse this DataGridViewCellParsingEventArgs instance.
                    e.ParsingApplied = false;
                }
            }
        }
    }
}

.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
Show:
© 2015 Microsoft