TechAid24

Your Technical Consultant

Step-by-Step Guide: Copy Specific Columns Between DataTables in C#

Posted on by Selim Ahmed 1 minute read

In this tutorial, I’ll show you how to copy specific columns from one datatable to another datatable in c#.

Suppose, I have a complex store procedure in my SQL Database, which returns 25 columns with a large amount of data. I use the store procedure in multiple forms so that I can’t change the store procedure. I want to copy specific columns from the DataTable to another in C#. I follow the method and it’s working fine.

public DataTable LoadMyData(Hashtable ht)
{
      _dbTable = new DataTable();
      _reader = new DataReader();
      DataTable _dt = _reader.GetDataTableByCommandFromStoredProc("SP_ATTENDANCE", ht); //get datatable by execute store procedure

      String[] columnList = new String[] { "AttnDate", "Status", "ActualInTime", "ActualOutTime", "OutStatus" }; //list of my specific columns 
      return _dbTable = new DataView(_dt).ToTable(false, columnList);
}
Copy specific columns from one DataTable to another

Selim Ahmed

Thank you for visiting my personal blog. I’m Selim Ahmed, a tech-savvy who loves to learn a new technology every day. Innovation, research, and development is my daily activities. I love to work in a place where I can learn, innovate, and develop new things.Please feel free to reach me on any comments and feedback you have. I would be more than glad to listen and reply.

Leave a Reply

Your email address will not be published. Required fields are marked *