Q: copy of a datarow

Discussion in 'ASP .Net' started by =?Utf-8?B?SklNLkgu?=, Dec 14, 2005.

  1. 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,
     
    =?Utf-8?B?SklNLkgu?=, Dec 14, 2005
    #1
    1. Advertising

  2. Re: copy of a datarow

    "JIM.H." <> wrote in message
    news:...
    > 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
     
    Marius Tennes Krogh, Dec 14, 2005
    #2
    1. Advertising

  3. Re: copy of a datarow

    Try this:

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

    Eliyahu

    "JIM.H." <> wrote in message
    news:...
    > 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,
    >
    >
     
    Eliyahu Goldin, Dec 14, 2005
    #3
  4. Re: copy of a datarow

    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" <> wrote in message
    news:...
    >
    > "JIM.H." <> wrote in message
    > news:...
    >> 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
    >
    >
     
    Eliyahu Goldin, Dec 14, 2005
    #4
  5. =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Dec 14, 2005
    #5
  6. Re: copy of a datarow

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >>
    >> "JIM.H." <> wrote in message
    >> news:...
    >>> 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
    >>
    >>

    >
    >
     
    Marius Tennes Krogh, Dec 14, 2005
    #6
  7. Re: copy of a datarow

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

    Eliyahu

    "Marius Tennes Krogh" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    >> 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" <> wrote in message
    >> news:...
    >>>
    >>> "JIM.H." <> wrote in message
    >>> news:...
    >>>> 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
    >>>
    >>>

    >>
    >>

    >
    >
     
    Eliyahu Goldin, Dec 15, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RmF0Ym95Q2FudGVlbg==?=

    DataRow Copy

    =?Utf-8?B?RmF0Ym95Q2FudGVlbg==?=, Jan 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    835
    =?Utf-8?B?RmF0Ym95Q2FudGVlbg==?=
    Jan 30, 2004
  2. ypul
    Replies:
    3
    Views:
    41,738
  3. Bennett Haselton
    Replies:
    0
    Views:
    695
    Bennett Haselton
    Nov 11, 2004
  4. Shawn
    Replies:
    3
    Views:
    30,470
    Robbe Morris [C# MVP]
    Mar 9, 2005
  5. Imran Aziz

    copy datarow to string array

    Imran Aziz, Mar 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    9,947
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 17, 2006
Loading...

Share This Page