Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get all rows from data table for particular column in c#
Posted
Updated 23-Apr-13 1:35am
v3

Datatable as in a database? Then you could use a SqlCommand and a DataReader to get all the values. (in this example as strings)

C#
var values = new List<string>();
var conn = new SqlConnection("connectionstring");
using (var cmd = new SqlCommand("select col1 from myTable", conn))
{
    conn.Open();
    var dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        values.Add(dr.GetString(0));
    }
    conn.Close();
}


Or if it's from a DataTable object:
C#
List<string> values = tbl.AsEnumerable().Select(r => r.Field<string>("MyColumn")).ToList();
 
Share this answer
 
v2
Comments
kiran pericherla 23-Apr-13 7:44am    
Thank you it's working
StianSandberg 23-Apr-13 7:46am    
you're welcome :)
Try this:-

String col="LastName";

DataTable tableWithOnlySelectedColumns =
    new DataView(table).ToTable(false, col);
 
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