Home > DeveloperSection > Forums > How to get records of specific columns of data table using c#.
James Smith

Total Post:48

Posted on    July-23-2011 4:38 AM

 C# C# 

 2 Reply(s)
 48212  View(s)
Rate this:


I have a small problem while making a program in c#. The problem is that I would like to access record of selected columns of data table in c#. Like suppose I have a data table which have four columns namely empId, empName, empAge and empCity . Now I would like to select record of only two columns of this data table. This is prity simple in sql query but how can I perform it into data table.

Help me!


Awadhendra Tiwari

Total Post:126

Posted on    July-23-2011 4:46 AM

Hi james,

You can get information of selected columns in following way,

string[] selectedColumns = new[] { "ID", "LastName" };  //It represent name of column for which you want to select records

DataTable tableWithOnlySelectedColumns = new DataView(table).ToTable(false, selectedColumns);

//Here table represent name of that DataTable which have all columns.


Please mark it as answer if your problem had been resolved.

Alex R
Alex R

Total Post:40

Posted on    July-23-2011 1:04 PM

Hi James,

You can easily select records of selected columns in DataTable. Check the code below.

DataTable dtSelectedColumns = dtOriginal.DefaultView.ToTable(false, "Column1", "Column2");

If you want those columns with distinct records then pass first parameter true except false. You can also provide as many column as you want in the parameter.

Modified On Jul-23-2011 01:06:53 PM

Don't want to miss updates? Please click the below button!

Follow MindStick