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

后端 未结 4 993
借酒劲吻你
借酒劲吻你 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:45

    Also may this code serve someone! I have a realize that this is the easiest way just like @Henk said:

    using (SqlDataAdapter da = new SqlDataAdapter())
                        {
                            DataTable dt = new DataTable();
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            da.SelectCommand = cmd;
                            da.Fill(dt);
                            dt.Columns[0].ColumnName = "Item NO";
                            dt.Columns[1].ColumnName = "LocalCode";                       
                            dt.Columns[2].ColumnName = "Currency";
                            dt.Columns[3].ColumnName = "Menu Flag";
                            dt.Columns[4].ColumnName = "Item Class";
                            dt.Columns[4].ColumnName = "Dress Sort";
                            return dt;
                        }
    

提交回复
热议问题