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

1 solution

Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2018
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