Creating the first row in a DataTable column name

I am trying to set the first row of a DataTable as column names. I am using the GenericParsing library from a code project. The problem is that the parser sets the default column name.

Thanks.

+4
source share
3 answers

This should do what you want:

DataRow firstRow = table.NewRow(); List<string> names = new List<string>(); foreach (DataColumn column in table.Columns) { names.Add(column.ColumnName); } firstRow.ItemArray = names.ToArray(); table.Rows.InsertAt(firstRow, 0); 

If the first line already exists and you want to β€œoverwrite” this change, the first line:

 DataRow firstRow = table.Rows[0]; 

And delete the last line.

+6
source

I think you need the following:

 foreach (DataColumn column in table.Columns) { string cName = table.Rows[0][column.ColumnName].ToString(); if (!table.Columns.Contains(cName) && cName != "") { column.ColumnName = cName; } } table.Rows[0].Delete(); //If you don't need that row any more 
+9
source

hi, hope this helps u, it will make the whole row as a column header

 foreach (DataRow inRow in inputTable.Rows) { string newColName = inRow[0].ToString(); outputTable.Columns.Add(newColName); } 
0
source

Source: https://habr.com/ru/post/1389075/


All Articles