Click here to Skip to main content
14,239,810 members
Rate this:
Please Sign up or sign in to vote.
I have a DataTable with multiple columns. I want to get a List<String> of all column of DataTable. How can I do that?

List<string> fields;
foreach (DataRow dr in dt.Rows)

how can i do this??
CodeBlack 22-Aug-13 4:38am
Do you want to all rows of all columns into one list List<string> ?
rahul55555 22-Aug-13 4:41am
yeah.. actualy i already have 1st column data into list<string> fields;
the remaining columns are in datatable. i want to get those into list and bind list to gridview
Mohan Gopi 22-Aug-13 6:40am
You know Datatable column Names?... If you know means use below code

List<string> NameList = (from r in dt.AsEnumerable()
select r.Field<datatypeofcolumnname1>("ColumnName1") + r.Field<datatypeofcolumnname2>("ColumnName2")).ToList();

i hope this will solve your problem
rahul55555 22-Aug-13 6:45am
yeah i know..
Mohan Gopi 22-Aug-13 6:47am
Use this code ..

List NameList = (from r in dt.AsEnumerable()
select r.Field("ColumnName1") + r.Field("ColumnName2")).ToList();

Let me know..
rahul55555 22-Aug-13 6:57am
List<string> Namelist = (from dr in dt.AsEnumerable() select dr.Field("Tran_DateTime") + dr.Field("Tran_UniqueNum")).ToList();

please check what is the error in this..
Mohan Gopi 22-Aug-13 6:59am
Here the field datatype tag is not taking, so i will add my solution in below yar.. kindly check it.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use Below Code :

List<string> fields = new List<string>();

for (int column = 0; column < dt.Columns.Count; column++)
  for (int row = 0; row < dt.Rows.Count; row++)
rahul55555 22-Aug-13 4:55am
sory, i am gettin data in only 1 column one after the other
CodeBlack 22-Aug-13 4:58am
ok. so you want all the columns into separate lists ? and you want to bind those lists into gridview right ? If you want to do this than you can directly assign DataTable to GridView's DataSource.
rahul55555 22-Aug-13 5:04am
yeah i can do that.. but i have 1 column in list. how to bind both datatable and list to gridview
rahul55555 22-Aug-13 5:00am
not working..
8/20/2013 10:41:41 AM
8/20/2013 10:07:29 AM
8/20/2013 10:04:57 AM
8/20/2013 9:27:42 AM
8/20/2013 10:03:33 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
8/20/2013 12:00:00 AM
i am gettin like this in 1 single column
CodeBlack 22-Aug-13 5:20am
i am not getting clear about your question. But if you want to bind list and datatable then if there is a mapping between those two collections then you can create a new property class (Entity Class) and then after you can assign those values to the property class.

Another option is to add new column into datatable as mentioned below :

dt.Columns.Add(new DataColumn("NewColumn"));
for (int i = 0; i < list.Count; i++)
if (dt.Rows.Count < i + 1)
DataRow dr = dt.NewRow();

dt.Rows[i]["NewColumn"] = list[i];
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi Rahul, try this

List<string> NameList = (from r in dt.AsEnumerable()
select r.Field<Column1DataType>("Column1") + r.Field<Column2DataType>("Column2")).ToList();

i hope this will help you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100