HOW TO GET RECORDS OF SPECIFIC COLUMNS OF DATA TABLE USING C#.

James Smith

Total Post:48

Points:336
Posted by  James Smith
C# 
C#
 48382  View(s)
Ratings:
Rate this:

Hi,

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!

Thanks.

  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: How to get records of specific columns of data table using c#.

    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.

    Thanks.

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


  1. Alex R

    Post:40

    Points:280
    Re: How to get records of specific columns of data table using c#.

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!