Copying a Row in a datagrid

D

Dean Landry

Hello,

I am trying to duplicate a row in my datagrid. Below is my code. After I
duplicate the row I manually change one of the primary key fields so that
there isn't a problem with duplicate rows. But from time to time, after
using the function below, I get the "The Changes you requested to the table
were not successful because they would create duplicate values in the index,
primary key, or relationship" when I update the dataset the dataview is
based on. When the error occurs, there are no rows in the datagid that have
duplicate keys. Does anyone know where the problem might lie? Also, is my
function below a good way to duplicate a row in my datagrid?

Thanks,

Dean

try
{
// create a new datarow on the dataview (myDV)
DataRowView drv = myDV.AddNew();
drv.BeginEdit();
// these three columns compose the primary key
drv.Row["ScheduleName"] =
myDV[this.dataGrid1.CurrentRowIndex]["ScheduleName"];
drv.Row["Buss"] = myDV[this.dataGrid1.CurrentRowIndex]["Buss"];
drv.Row["Time"] = myDV[this.dataGrid1.CurrentRowIndex]["Time"];
// the rest of the values
drv.Row["Duration"] = myDV[this.dataGrid1.CurrentRowIndex]["Duration"];
drv.Row["Approx"] = myDV[this.dataGrid1.CurrentRowIndex]["Approx"];
drv.Row["Man"] = myDV[this.dataGrid1.CurrentRowIndex]["Man"];
drv.Row["Source"] = myDV[this.dataGrid1.CurrentRowIndex]["Source"];
drv.Row["Fade"] = myDV[this.dataGrid1.CurrentRowIndex]["Fade"];
drv.Row["Item"] = myDV[this.dataGrid1.CurrentRowIndex]["Item"];
drv.Row["ItemDesc"] = myDV[this.dataGrid1.CurrentRowIndex]["ItemDesc"];
drv.Row["ProgramTitle"] =
myDV[this.dataGrid1.CurrentRowIndex]["ProgramTitle"];
drv.Row["CRTC1"] = myDV[this.dataGrid1.CurrentRowIndex]["CRTC1"];
drv.Row["CRTC2"] = myDV[this.dataGrid1.CurrentRowIndex]["CRTC2"];
drv.Row["CRTC3"] = myDV[this.dataGrid1.CurrentRowIndex]["CRTC3"];
drv.Row["ProductionPoint"] =
myDV[this.dataGrid1.CurrentRowIndex]["ProductionPoint"];
drv.Row["Sil1"] = myDV[this.dataGrid1.CurrentRowIndex]["Sil1"];
drv.Row["Sil2"] = myDV[this.dataGrid1.CurrentRowIndex]["Sil2"];
drv.EndEdit();
this.dataGrid1.Update();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
 
C

Cor

Hi Dean,

If there are duplicated rows in the datagrid is not important, that is only
a human view on your datatable, also not if there are duplicated rows in the
dataview, because that only reflects the grid as a selection from the
datatable.

Important is that the datatable underlaying the dataview holds no duplicate
keys.

Just my thought,

Cor
 

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

Top