473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Q: copy of a datarow

Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy of
this row as drCopy and change a few fields and add both to the dataset, how
can I do this?
Thanks,
Dec 14 '05 #1
6 1619

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy
of
this row as drCopy and change a few fields and add both to the dataset,
how
can I do this?
Thanks,


You can create your own little method for copying a row:

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
Marius
Dec 14 '05 #2
Try this:

DataRow drCopy= dataSet11.myTable.NewRow();
drCopy.ItemArray = dr.ItemArray;

Eliyahu

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy
of
this row as drCopy and change a few fields and add both to the dataset,
how
can I do this?
Thanks,

Dec 14 '05 #3
Marius,

dr[i] returns a reference to the cell. I am afraid in your solution dr and
drCopy will share the same set of cells.

Eliyahu

"Marius Tennes Krogh" <ma***@online.no> wrote in message
news:uH*************@tk2msftngp13.phx.gbl...

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy
of
this row as drCopy and change a few fields and add both to the dataset,
how
can I do this?
Thanks,


You can create your own little method for copying a row:

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
Marius

Dec 14 '05 #4
http://msdn.microsoft.com/library/de...rtrowtopic.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"JIM.H." wrote:
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy of
this row as drCopy and change a few fields and add both to the dataset, how
can I do this?
Thanks,

Dec 14 '05 #5
Are you shure about that? Because the code works fine for me. Here's one
example:
----------------------
private void Page_Load(object sender, System.EventArgs e)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("col1", typeof(int));
_dt.Columns.Add("col2", typeof(string));
_dt.Columns.Add("col3", typeof(string));

DataRow dr = _dt.NewRow();
dr[0] = 1;
dr[1] = "some text";
dr[2] = "one";

DataRow drCopy = CopyRow(dr);
drCopy[0] = 2;
drCopy[2] = "two";

_dt.Rows.Add(dr);
_dt.Rows.Add(drCopy);

DataGrid1.DataSource = _dt;
DataGrid1.DataBind();
}

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
----------------------------------
The result for the grid is this:
col1 col2 col3
1 some text one
2 some text two

----------------------------------
Marius
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Marius,

dr[i] returns a reference to the cell. I am afraid in your solution dr and
drCopy will share the same set of cells.

Eliyahu

"Marius Tennes Krogh" <ma***@online.no> wrote in message
news:uH*************@tk2msftngp13.phx.gbl...

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a copy
of
this row as drCopy and change a few fields and add both to the dataset,
how
can I do this?
Thanks,


You can create your own little method for copying a row:

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
Marius


Dec 14 '05 #6
Apparently you are right. dr[i] returns the data value rather than a
reference to the cell object.

Eliyahu

"Marius Tennes Krogh" <ma***@online.no> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Are you shure about that? Because the code works fine for me. Here's one
example:
----------------------
private void Page_Load(object sender, System.EventArgs e)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("col1", typeof(int));
_dt.Columns.Add("col2", typeof(string));
_dt.Columns.Add("col3", typeof(string));

DataRow dr = _dt.NewRow();
dr[0] = 1;
dr[1] = "some text";
dr[2] = "one";

DataRow drCopy = CopyRow(dr);
drCopy[0] = 2;
drCopy[2] = "two";

_dt.Rows.Add(dr);
_dt.Rows.Add(drCopy);

DataGrid1.DataSource = _dt;
DataGrid1.DataBind();
}

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
----------------------------------
The result for the grid is this:
col1 col2 col3
1 some text one
2 some text two

----------------------------------
Marius
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Marius,

dr[i] returns a reference to the cell. I am afraid in your solution dr
and drCopy will share the same set of cells.

Eliyahu

"Marius Tennes Krogh" <ma***@online.no> wrote in message
news:uH*************@tk2msftngp13.phx.gbl...

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hello

I have;
DataRow dr= dataSet11.myTable.NewRow();

And I am filling the fields of this datarow. Now I need to create a
copy of
this row as drCopy and change a few fields and add both to the dataset,
how
can I do this?
Thanks,

You can create your own little method for copying a row:

private DataRow CopyRow(DataRow dr)
{
DataRow drCopy = dr.Table.NewRow();
for(int i=0;i<dr.Table.Columns.Count;i++)
drCopy[i] = dr[i];

return drCopy;
}
Marius



Dec 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kelvin | last post by:
Hi All, Due to can't insert new row between row and row while current DataTable looping. In order to solve the problem, I need to clone it as new DataTable, while the DataTable Looping, it also...
3
by: ypul | last post by:
can anyone tell me please .. how to copy one datarow from one datatable to another.. I want to extract some specific rows from one datatable and put into antoher .... "...
0
by: Bennett Haselton | last post by:
If I have just filled a DataTable in a typed DataSet with a single row, is there a way I can make a copy of that row, so that I can clear the DataTable in that DataSet and use it for another query,...
1
by: SunshineInTheRain | last post by:
Dim dtUn As DataTable dtUn= New DataTable dtUn = BindICUN() Dim i As Integer Dim irow As DataRow Dim mailBody As String = "" Dim myRow As...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.