Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
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)
            {
                fields.Add(row[0].ToString());
            }

how can i do this??
Posted
Comments
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..

Use Below Code :

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

for (int column = 0; column < dt.Columns.Count; column++)
{
  for (int row = 0; row < dt.Rows.Count; row++)
  {
    fields.Add(dt.Rows[row][column].ToString());
  }
}
 
Share this answer
 
v3
Comments
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
456
5648
5752
12345
24542
180063442435
180065242435
180065342435
184563442435
184563474435
524563234435
524563474435
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.Add(dr);
}

dt.Rows[i]["NewColumn"] = list[i];
}
Hi Rahul, try this


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


i hope this will help you...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900