By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Q: copy of a datarow

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.