Sujeet Raman

Sujeet Raman

  • 859
  • 927
  • 357.5k

How to set a data table column as a first column of a Data Table

Dec 8 2021 6:09 PM

Hi, I have a data table having data from DB

I need to add one more column and data to that table from a .txt file.

when i ran my code  that new colum  not coming as a first colum and and a extra column without any data is added in to the data table.

Please anyone help 

my .txt file is like

NAME       
----  
val1      
val3      
val5

i need to have  NAME column as 0th colum of data table.below is my code

if (tableName == "NewData")
{var pathC = @"D:\files\Data\names.txt";var fileContents = File.ReadAllLines(pathC);var splitFileContents = (from f in fileContents select f.Split(':')).ToArray();int maxLength = (from s in splitFileContents select s.Count()).Max();for (int i = 0; i < maxLength; i++)
{
table.Columns.Add();
}foreach (var line in splitFileContents)
{
DataRow row = table.NewRow();
row.ItemArray = (object[])line;
table.Rows.Add(row);
}
return table; //NAMES column will be the first column}

 


Answers (12)