Deleting columns from a DataTable

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
 
J

Joe Fallon

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top