Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
Posted 25-Jan-13 7:44am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The DataGridView has a source (you assigned it to ds.Tables[0]) so it gets its rows from the source, not the internal row collection. If you want to add rows, you need to add them to the source, not the DataGridView, so that the DataGridView can then display them.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100