Hi everyone!
I have a question regarding how to add rows programmatically to a datagridview. I have a button that is supposed to clone a highlighted row.
Here is what I have to fill the datagridview.
SqlCeConnection conn = new SqlCeConnection("Data Source=|DataDirectory|\\waveform_db.sdf");
string commString = "SELECT Vehicles.MAKE, Vehicles.MODEL, Vehicles.YEAR, Vehicles.ENGINE_CODE, Vehicles.TRANSMISSION_CODE, EngineAndTransmissionCodes.ENGINE_CONTROL, " +
"EngineAndTransmissionCodes.CAMCRANK, EngineAndTransmissionCodes.HYBRID, EngineAndTransmissionCodes.TRANSMISSION, " +
"EngineAndTransmissionCodes.AIR_CONDITIONING, EngineAndTransmissionCodes.ALTERNATOR, EngineAndTransmissionCodes.SMART_KEY, " +
"EngineAndTransmissionCodes.CRUISE_CONTROL, EngineAndTransmissionCodes.IMMOBILIZER, EngineAndTransmissionCodes.TELEMATICS, " +
"EngineAndTransmissionCodes.MOST, EngineAndTransmissionCodes.LAN, EngineAndTransmissionCodes.LIN, EngineAndTransmissionCodes.MPX, " +
"EngineAndTransmissionCodes.SIL, EngineAndTransmissionCodes.CAN, EngineAndTransmissionCodes.REAR_VIEW_CAMERA, " +
"EngineAndTransmissionCodes.PARKING_ASSIST, EngineAndTransmissionCodes.DVD_PLAYER " +
"FROM Vehicles INNER JOIN " +
"EngineAndTransmissionCodes ON Vehicles.ENGINE_CODE = EngineAndTransmissionCodes.ENGINE_CODE AND " +
"Vehicles.TRANSMISSION_CODE = EngineAndTransmissionCodes.TRANSMISSION_CODE";
SqlCeDataAdapter scda = new SqlCeDataAdapter(commString, conn);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
conn.Open();
scda.Fill(dt);
ds.Tables.Add(dt);
conn.Close();
masterLayoutDGview.DataSource = ds.Tables[0];
That fills the datagridview fine. Here is what I have to clone a row and add it to the grid.
int selectedRowIndex = this.masterLayoutDGview.SelectedCells[0].RowIndex;
int i = this.masterLayoutDGview.Rows.AddCopy(selectedRowIndex);
this.masterLayoutDGview.Rows[i].Cells[0].Value = this.masterLayoutDGview.Rows[selectedRowIndex].Cells[0].Value;
this.masterLayoutDGview.Rows[i].Cells[1].Value = this.masterLayoutDGview.Rows[selectedRowIndex].Cells[1].Value;
The error I am receiving when I click the button associated with cloning a highlighted row is:
InvalidOperationException:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Can anyone shed some light as to what I'm doing wrong?
Thanks everyone.