Click here to Skip to main content
13,091,145 members (51,265 online)
Rate this:
Please Sign up or sign in to vote.
See more:
how to get all rows from data table for particular column in c#
Posted 23-Apr-13 1:29am
Updated 23-Apr-13 1:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

var values = new List<string>();
var conn = new SqlConnection("connectionstring");
using (var cmd = new SqlCommand("select col1 from myTable", conn))
    var dr = cmd.ExecuteReader();
    while (dr.Read())

Or if it's from a DataTable object:
List<string> values = tbl.AsEnumerable().Select(r => r.Field<string>("MyColumn")).ToList();
kiran pericherla 23-Apr-13 7:44am
Thank you it's working
AlluvialDeposit 23-Apr-13 7:46am
you're welcome :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:-

String col="LastName";
DataTable tableWithOnlySelectedColumns =
    new DataView(table).ToTable(false, col);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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