开发者

how to convert datarow into datatable

开发者 https://www.devze.com 2022-12-11 00:56 出处:网络
DataRow[] row = table.Select(\"Weight=\'57\'\");//has1record DataTable dt = new DataTable(); foreach (DataRow dr in row)

DataRow[] row = table.Select("Weight='57'");// has 1 record DataTable dt = new DataTable();

     foreach (DataRow dr in row)
                {
                    dt.ImportRow(dr);
                }
                dt.A开发者_JS百科cceptChanges();

my row has 1 record. when i try to convert datarow[] row into datatable it runs fine. when i check my table dt it does not contain any record in it . what is the issue in it

thank you


DataTable dt = new DataTable();

DataRow[] dr = dt.Select("Your string");

DataTable dt1 = dr.copyToDataTable();


You should copy the row into a new row, and than add it to the new table. you can't take a row from one table and put it on another table.

Code Sample from another site:

DataTable dtDest = new DataTable();
dtDest = dsActivity.Tables[0].Clone();
foreach(DataRow dr in dsSrc.Tables[0].Rows)
{
 DataRow newRow = dtDest .NewRow();
 newRow.ItemArray = dr.ItemArray;
 dtDest.Rows.Add(newRow);
}

the clone makes the dtDest the same type (columns) as the source.

The other way is to create a new row, add the right columns, and copy value by value.


DataRow[] rows = table.Select("Weight='57'");
DataTable dt = new DataTable();

foreach (DataRow item in rows)
{
    // add values into the datatable
    dt.Rows.Add(item.ItemArray);
}


DataTable DT = DTMain.Clone();
// suppose DT is the table where we need to put datarows and DTMain is a table
// having the stucture of the datatbale, means you must have some structure for
// datatable.

//supposes you have a collection of datarows as

foreach(DataRow dr in DatarRowColl)
{
    DT.ImportRow(dr);
}

DT.AcceptChanges();

// here you get the datarows in a datatable.

Happy Programming.


Try the following:

dt.Rows.Add(dr)


dim dt as new DataTable
dim row as new DataRow
dim params(10) as string
''''' imagine we have row and we fill it
''''' you should create your dataTable as you want
dt.Columns.Add("column1")
dt.Columns.Add("column2")
.
.
.
For Each r As DataRow In row
    params = {dr.Item("column1"), dr.Item("column2")}
    dt.Rows.Add(params)
Next
dt.AcceptChanges()
0

精彩评论

暂无评论...
验证码 换一张
取 消