Search code examples
c#asp.netlinqdatatable

How to rename the datatable column name without losing the data?


I want to rename my DataTable column names.

I tried this:

dt.Columns[8].ColumnName = "regnum";

dt.AcceptChanges();

but my data is lost afterwards.


Solution

  • dt.Columns[8].ColumnName = "regnum";
    

    This just binds your Columns[8] to the non-existing "regnum" column in the Db.

    If you want to rename the actuals Db column, execute an SQL script.

    But my guess is you actually want to change the Caption:

      dt.Columns[8].Caption = "regnum";