Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
how to get all rows from data table for particular column in c#
Posted 23-Apr-13 2:29am
Edited 23-Apr-13 2:35am
v3
Rate this: bad
good
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))
{
    conn.Open();
    var dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        values.Add(dr.GetString(0));
    }
    conn.Close();
}
 
Or if it's from a DataTable object:
List<string> values = tbl.AsEnumerable().Select(r => r.Field<string>("MyColumn")).ToList();
  Permalink  
v2
Comments
kiran pericherla at 23-Apr-13 7:44am
   
Thank you it's working
AlluvialDeposit at 23-Apr-13 7:46am
   
you're welcome :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:-
 
String col="LastName";
 
DataTable tableWithOnlySelectedColumns =
    new DataView(table).ToTable(false, col);
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Afzaal Ahmad Zeeshan 82
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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