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

后端 未结 4 964
借酒劲吻你
借酒劲吻你 2021-01-12 17:41

Q:

I want to rename my data table column names .

I tried this :

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

dt.AcceptChanges();
<
4条回答
  •  执笔经年
    2021-01-12 18:41

    Following is the example:

    DataTable Dt = new DataTable();
    DataColumn Dc = new DataColumn("Name");
    DataColumn Dc1 = new DataColumn("ID");
    Dt.Columns.Add(Dc);
    Dt.Columns.Add(Dc1);
    
    DataRow dr = Dt.NewRow();
    dr["name"] = "1";
    dr["ID"] = "111";
    Dt.Rows.Add(dr);
    
    dr = Dt.NewRow();
    dr["name"] = "2";
    dr["ID"] = "11112";
    Dt.Rows.Add(dr);
    
    Dt.Columns[0].ColumnName = "ddsxsd";
    Dt.AcceptChanges();
    

    I did not find any data loss!!!!!!!! Because it will merely change the column name.

    EDIT

    You can also bring your desired column names from your Stored Procedures.

提交回复
热议问题