spacer.png, 0 kB
spacer.png, 0 kB
Home arrow Tech Blog arrow C# - How to get column names of a Datatable?

C# - How to get column names of a Datatable?

User Rating: / 34
PoorBest 
Tuesday, 19 July 2011

Pass the name of a datatable and this function will return the column names contactenated with ","

 public static string GetColumnNames(DataTable table)
{
if (table != null)
{
List lstColNames =
(from DataColumn col in table.Columns
select col.ColumnName).ToList();
 
return String.Join(",", lstColNames.ToArray());
}
 
return string.Empty;
}
 

Comments  

 
+3 #2 MoaviaH. 2012-03-30 19:20
Simply use for each loop instead of LINQ query. See below:

public static string GetColumnNames( System.Data.DataTable table)
{
if (table != null)
{
List lstColumn = new List();

foreach (System.Data.DataColumn col in table.Columns)
{
lstColumn.Add(col.ColumnName);
}

return String.Join(",", lstColNames.ToArray());
}

return string.Empty;
}
Quote
 
 
+2 #1 Edu 2012-03-30 16:35
it is possible to get this information when you are workin on 2.0 framework??

Many thanks
Quote
 

Add comment


Security code
Refresh

spacer.png, 0 kB
spacer.png, 0 kB
spacer.png, 0 kB