用 ac# 数据表的第一行覆盖列标题

我想知道是否存在一种有效的方法来首先覆盖a的column标题datatablerow

为了说明我的观点,假设一个有,作为一个样本datatable

Column 0 Column 1  Column 2

--------------------------

x1        x2        x3

事实上,它可以使用

dataTable.Columns["Column 0"].ColumnName = "x1";

dataTable.Columns["Column 1"].ColumnName = "x2"; 等等

dataTable.AcceptChanges();

但是,不可否认,如果处理50或,此手动更新将变得低效100 headers

提前致谢。


Helenr
浏览 116回答 1
1回答

慕尼黑5688855

您可以使用以下代码:using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;using System.Data;namespace Rextester{&nbsp; &nbsp; public class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Your code goes here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Hello, world!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable table = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Dosage", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Drug", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Patient", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //table.Columns.Add("Date", typeof(DateTime));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Here we add five DataRows.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(25, "Indocin", "David");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(50, "Enebrel", "Sam");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(10, "Hydralazine", "Christoff");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(21, "Combivent", "Janet");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(100, "Dilantin", "Melanie");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(table.Rows.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < table.Columns.Count; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string columnName = Convert.ToString(table.Columns[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Columns[columnName].ColumnName = Convert.ToString(table.Rows[0][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < table.Columns.Count; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(table.Columns[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP