ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

RemoveColumn

Code that allows deleting a column stating the name

Source

public static void RemoveColumn(this System.Data.DataTable dt, string columnName)
{
    if (dt != null && !string.IsNullOrEmpty(columnName) && dt.Columns.IndexOf(columnName) >= 0)
    {
        int idx = dt.Columns.IndexOf(columnName);
        dt.Columns.RemoveAt(idx);
        dt.AcceptChanges();
    }
}

Example

// Consider that dataSetName is the DataSet that contains a DataTable
dataSetName.Tables[0].RemoveColumn("Column12");

Author: Raphael Augusto Ferroni Cardoso

Submitted on: 4 jun 2014

Language: C#

Type: System.Data.DataTable

Views: 2789