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