DataGridViewColumnCollection.Remove Method

Definition

Removes a column from the collection.

Overloads

Remove(String)

Removes the column with the specified name from the collection.

Remove(DataGridViewColumn)

Removes the specified column from the collection.

Remove(String)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Removes the column with the specified name from the collection.

C#
public virtual void Remove(string columnName);

Parameters

columnName
String

The name of the column to delete.

Exceptions

columnName does not match the name of any column in the collection.

columnName is null.

The associated DataGridView control is performing one of the following actions that temporarily prevents new columns from being added:

  • Selecting all cells in the control.

  • Clearing the selection.

  • Updating column DisplayIndex property values.

-or-

This method is being called from a handler for one of the following DataGridView events:

Examples

The following code example illustrates the use of this method. For more information, see How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control.

C#
private void BindDataAndInitializeColumns()
{
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = customersDataSet;
    dataGridView1.Columns.Remove("Fax");
    dataGridView1.Columns["CustomerID"].Visible = false;
}

Remarks

The name of a column is indicated by the DataGridViewColumn.Name property.

To remove a column that is automatically generated when binding to a data source, call this method in a DataGridView.DataBindingComplete event handler.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Remove(DataGridViewColumn)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Removes the specified column from the collection.

C#
public virtual void Remove(System.Windows.Forms.DataGridViewColumn dataGridViewColumn);

Parameters

dataGridViewColumn
DataGridViewColumn

The column to delete.

Exceptions

dataGridViewColumn is not in the collection.

dataGridViewColumn is null.

The associated DataGridView control is performing one of the following actions that temporarily prevents new columns from being added:

  • Selecting all cells in the control.

  • Clearing the selection.

  • Updating column DisplayIndex property values.

-or-

This method is being called from a handler for one of the following DataGridView events:

Remarks

To remove a column that is automatically generated when binding to a data source, call this method in a DataGridView.DataBindingComplete event handler.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9