Deleting columns from a DataTable

  • Thread starter Thread starter Thomas Brown
  • Start date Start date
T

Thomas Brown

I have a DataTable that I've filled from a query, it has five columns
and eight rows. Three of the columns comprise the primary key and I
want to remove two of them.

Here is what I'm trying and it is almost working...

table.PrimaryKey = null; // so we can delete primary key columns
Columns.RemoveAt(0); // Remove the first column
Columns.RemoveAt(0); // Remove what was the second column.

table.PrimaryKey = new DataColumn[1] {Columns[2]};

I have a DataGrid bound to the table and it reflects the dropped
columns just fine. Debugging, I can see the data rows have been
updated automatically, the first two values in the ItemArray of each
row having been deleted.

However, when I try to modify a data cell now in the DataGrid, I'm
getting an exception thrown, "Cannot set column 3". As far as I can
tell, this has something to do with the fact that the table still has
index entries that reference now non-existant columns. What do I have
to do to get the table to refresh its indices? There appear to be no
properties or methods that give me access to this portion of a
DataTable.

Thanks in advance,
-- Thomas Brown
 
Did you try re-setting the datagrid to the new table?

(Not sure why you don't delete the columns first and then display the grid.)
 
Back
Top