locked
add column to datatable RRS feed

  • Question

  • User1185541504 posted

    I have a datatable that contains data from the database.  Now I want to programatically add a column to the datatable in the first position.  Is there a way to do this without creating a new datatable, adding the column then copying the existing datatable to the new datatable?

    Tuesday, August 28, 2007 11:05 AM

Answers

  • User-1426124666 posted

    In that case I think you would have to copy the contents to a new DataTable (you can use the Columns.CopyTo method to do this). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2007 3:55 AM

All replies

  • User-1426124666 posted

    Yes, you can use the Columns.Add method. 

    Tuesday, August 28, 2007 11:19 AM
  • User1185541504 posted

    Thanks for the reply - I'm using the Columns.Add method but it adds the column to the end of the datatable.  I want to add this as the first column to the datatable.  I don't see any methods that allow me to add a column in a certain position.

    Tuesday, August 28, 2007 11:34 AM
  • User-1426124666 posted

    Why does it have to be in a specific position? Can't the control that is displaying the data show the columns in the relevant order? 

    Tuesday, August 28, 2007 11:41 AM
  • User1185541504 posted

    Yes, I could do that.  But I'm working with existing code and it would be much easier to add the col to the beginning of the datatable than modify the output. You should see this code. Seems there's no way to do this other than creating a new datatable with that col as the first then copying the contents of the existing datatable to the new datatable.

    Thanks for you input.

    Tuesday, August 28, 2007 12:03 PM
  • User-1426124666 posted

    In that case I think you would have to copy the contents to a new DataTable (you can use the Columns.CopyTo method to do this). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2007 3:55 AM