Hi
I have below data in Datatable . I want to save the value of colum header in a variable i.e LeadNumber.
C#
foreach (DataRow colum in dt.Rows)
{| LeadNumber | LeadSource |
| 1 | Email Marketing |
| 2 | Website |
Thanks
Hi
I have below data in Datatable . I want to save the value of colum header in a variable i.e LeadNumber.
foreach (DataRow colum in dt.Rows)
{| LeadNumber | LeadSource |
| 1 | Email Marketing |
| 2 | Website |
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 9, 2023, 11:34 PM EDT
Hello Ramco,
You can access the value of the column header "LeadNumber" using the
Columnsproperty of theDataTableobject and then retrieve theColumnNameproperty of the correspondingDataColumnobject. Here's an example:string leadNumberColumnName = dt.Rows[0].Table.Columns[0].ColumnName;This will give you the string "LeadNumber", assuming that the "LeadNumber" column is the first column in the
DataTable. You can replace the index 0 with the actual index of the row that contains the column headers.Thanks
Naimish Makwana
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 8, 2023, 10:56 AM EDT
Hello,
You can get column header using below code,
string[] columnNames = dt.Columns.Cast() .Select(x => x.ColumnName) .ToArray();Above code will return all columns from your data table