Q: copy of a datarow

G

Guest

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,
 
M

Marius Tennes Krogh

JIM.H. said:
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 = dr;

return drCopy;
}


Marius
 
E

Eliyahu Goldin

Try this:

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

Eliyahu
 
E

Eliyahu Goldin

Marius,

dr 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 said:
JIM.H. said:
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 = dr;

return drCopy;
}


Marius
 
M

Marius Tennes Krogh

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 = dr;

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

----------------------------------
Marius


Eliyahu Goldin said:
Marius,

dr 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 said:
JIM.H. said:
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 = dr;

return drCopy;
}


Marius

 
E

Eliyahu Goldin

Apparently you are right. dr returns the data value rather than a
reference to the cell object.

Eliyahu

Marius Tennes Krogh said:
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 = dr;

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

----------------------------------
Marius


Eliyahu Goldin said:
Marius,

dr 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 said:
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 = dr;

return drCopy;
}


Marius


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top