有关以编程方式向datagridview添加行的问题

有关以编程方式向datagridview添加行的问题

问题描述:

大家好!



我有一个关于如何以编程方式将行添加到datagridview的问题。我有一个按钮,可以克隆突出显示的行。



这是我必须填写数据网格视图。



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];





填充datagridview很好。这是我必须克隆一行并将其添加到网格中。





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;





单击与克隆相关的按钮时收到的错误突出显示的行是:



InvalidOperationException:

当控件是数据时,无法以编程方式将行添加到DataGridView的行集合中绑定。



谁能解释一下我做错了什么?



谢谢每个人。



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.

DataGridView 有一个来源(你将它分配给 ds。表[0] )因此它从源获取其行,而不是内部行集合。如果要添加行,则需要将它们添加到源,而不是 DataGridView ,以便 DataGridView 然后可以显示它们。
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.