TableNewRow event handling issue

  • Thread starter Thread starter Bob Shiflett
  • Start date Start date
B

Bob Shiflett

Greetings,
Using VS 2005 SP1; I created a simple form by dragging and dropping a data
table. I changed it to a details view, so I have a mixture of text boxes,
combo boxes and a check box (no dgv). I want to programatically populate the
controls when the user clicks on the Add New button, so I added the following
event handler:

void MyDataTable_TableNewRow(object sender, DataTableNewRowEventArgs e)
{
int nIndex = int.Parse(this.bindingNavigatorPositionItem.Text);
e.Row.BeginEdit();
e.Row["Col0"] =
this.myDataSet.MyDataTable.Rows[nIndex].ItemArray[0].ToString();
e.Row["Col1"] =
this.myDataSet.MyDataTable.Rows[nIndex].ItemArray[1].ToString();
e.Row["Col2"] =
this.myDataSet.MyDataTable.Rows[nIndex].ItemArray[2].ToString();
...etc...
e.Row.EndEdit();
}

This works surprisingly well; the user can select any row and use it as a
template for the new row. I tried to take this a step further; instead of
setting the columns individually, I tried to use the CopyTo function as
follows:

void MyDataTable_TableNewRow(object sender, DataTableNewRowEventArgs e)
{
int nIndex = int.Parse(this.bindingNavigatorPositionItem.Text);
e.Row.BeginEdit();
this.myDataSet.MyDataTable.Rows[nIndex].ItemArray.CopyTo(e.Row.ItemArray,
0);
e.Row.EndEdit();
}

The code seems to execute, but all my controls are blank, like the CopyTo
did nothing; what am I missing here? Shouldn't this work?
 
Okay, so you don't need to use CopyTo, you can accomplish the same thing with
a simple assignment:

e.Row.ItemArray = this. myDataSet.MyDataTable.Rows[nIndex].ItemArray;

and it works great, but still, in all the searching I've done on this I've
seen other people having trouble with CopyTo, but I've seen no official
explanation or acknowledgement of this problem. Does anyone have any insight
on this? I'm just curious.....
 
Back
Top