Click here to Skip to main content
15,878,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
            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:

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.

1 solution

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.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900