Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove or hide particular column in a datatable?

Tags:

c#

I am using C#. I want to hide or remove the column from DataTable or DataSet . I attach my partial code:

DataTable dt = new DataTable();
DataView dv = new DataView();
dv = (DataView)Session["map_hi"];
dt = dv.ToTable();
dt.Columns[0].ColumnMapping = MappingType.Hidden;
dt.AcceptChanges();
like image 660
Bharathi Avatar asked Nov 16 '12 12:11

Bharathi


2 Answers

try this

   DataTable t;
   t.Columns.Remove("columnName");
   t.Columns.RemoveAt(columnIndex);
like image 60
Pranay Rana Avatar answered Oct 17 '22 06:10

Pranay Rana


As Pranay says you can remove columns as myTable.Columns.Remove("columnName");

But in my case it throws an exception as "Cannot remove this column, because it is part of the parent key for relationship"

I was able to overcome it as below.

  myTable.ParentRelations.Clear();
  myTable.ChildRelations.Clear();
  myTable.Constraints.Clear();
  myTable.Columns.Remove("columnName"); 

Hope this will help someone

like image 36
nuwancy Avatar answered Oct 17 '22 08:10

nuwancy