Click here to Skip to main content
14,920,592 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have one task to complete that is to convert datarow to datatable.
Ex. I have one datatable which contain 10 records.everytime when i read datarow, each row should be converted into datatable.

I used the below code but it is not working.

Updated 14-Jun-16 2:00am
Pheonyx 29-Nov-13 4:00am
"it is not working" is not a helpful statement.
a single line snippet is generally unhelpful when you are not being clear about what you are trying to do?

So, please use the "Improve Question" feature and provide a bit more detail about your actual objectives, bare in mind that a datarow cannot be converted to a datatable, a datatable is made up of datarows.

Are you getting any errors with the code line you are trying? Can you provide a slightly larger code snippet to get a bit more context to your problem? And can you explain your target objective as it might be that what you are trying to achieve hasn't been understood exactly.
JoCodes 29-Nov-13 5:12am
Post the relevant code .And, also make your question more clear. "each row converted to datatable"?Can you tell what you are trying to achieve

/// <summary>
/// Take each row from a source table and copy it to
/// a separate table. Return the new tables so created in a dataset.
/// </summary>
/// <param name="source">A datatable</param>
/// <returns></returns>
private DataSet SplitRows(DataTable source)

  DataSet ds = new DataSet();

  foreach(DataRow r in source.Rows) 
    DataTable copy = source.Clone();
    copy.LoadDataRow(r.ItemArray, true);

  return ds;

Before doing what you are trying to accomplish is you must know the difference between them. A DataSet contains DataTables and doesn't contain the actual data. A Datatable holds the data which basically contains DataRows.

Having that said, and to answer your question, you can't covert a DataRow to a DataTable or DataSet. You can however add DataRows to your DataTable. Here's a quick example how to add a DataRow to a new DataTable:

using System;
using System.Data;

namespace WebFormDemo
    public partial class DataTable : System.Web.UI.Page

        private DataTable CreateTable() {
            //this is just for demo. 
            //You may have to fill your datatable with data from database in real scenario

            DataTable dt = new DataTable();
            DataRow dr = null;

            //define the columns
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));

            //create new row
            dr = dt.NewRow();
            //add values to each rows
            dr["ID"] = 1;
            dr["FirstName"] = "Vincent";
            dr["LastName"] = "Durano";
            //add the row to DataTable

            //add new row
            dr = dt.NewRow();
            //add values to each rows
            dr["ID"] = 2;
            dr["FirstName"] = "Vianne";
            dr["LastName"] = "Durano";
            //add the row to DataTable

            //add new row
            dr = dt.NewRow();
            //add values to each rows
            dr["ID"] = 3;
            dr["FirstName"] = "Slash";
            dr["LastName"] = "TheMan";
            //add the row to DataTable

            return dt;

        private void FilterData(string filterValue) {
            DataTable dt = CreateTable(); //returns 3 rows
            DataTable dtTemp = CreateTable().Clone(); //copy the schema structure
            DataRow[] findRow = dt.Select(string.Format("LastName='{0}'",filterValue));

            foreach (DataRow row in findRow) {
                AddToTempTable(dtTemp, row);

            //print the result
            foreach (DataRow row in dtTemp.Rows) {
                Response.Write(string.Format("Name: {0} {1} <br />", row["FirstName"],row["LastName"]));


        private DataTable AddToTempTable(DataTable source, DataRow row) {
            DataRow newRow = source.NewRow();
            newRow[0] = row[0]; //ID
            newRow[1] = row[1]; //FirstName
            newRow[2] = row[2]; //LastName

            return source;
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack)

This would output the following in the page:

Name: Vincent Durano
Name: Vianne Durano
Richard Deeming 14-Jun-16 8:03am
This question was asked, and answered, three years ago. It only appeared in the "recent" list because someone decided to post a copy of solution 1 as a "new" solution.
Vincent Maverick Durano 14-Jun-16 8:07am
I must be blind for not seeing it. I didn't meant to bring back old threads to recent.

Thanks for letting me know. It's a big time waster for me to provide such answer. lol
Si tienes solo un DataRow como Origen y desdeas insertarlo en un DataTable vacío, simplemente podrías hacer esto:

yourEmptyDataTable= yourDataRowSource.Table.Clone();
OriginalGriff 3-Feb-17 11:47am
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 3 year old, solved ones.
Answering old questions - particularly in a non-English language - can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions, and use English (Google Translate can help here) and you'll be fine.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900