Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

前端 未结 3 706
余生分开走
余生分开走 2020-12-19 14:30

I have a grid and when data is loaded in grid; I simply select a row and press edit button. On edit, new sub form is opened and values of row\'s cells are passed to controls

3条回答
  •  星月不相逢
    2020-12-19 15:11

    Add or Edit your row in DataSource of your DataGridView. Do not directly add/edit to your grid.

    If your DataSource is DataSet and you want to add new row

    DataSet dsTempDataTable = (DataSet)MainApplication.dgvBooksDetails.DataSource;
    DataTable dt = dsTempDataTable.Tables[0]; // use table index/name to get the exact table
    DataRow dr = dt.NewRow();
    //  code to fill record
    dt.Rows.Add(dr);
    

    To Edit

    DataSet dsTempDataTable = (DataSet)MainApplication.dgvBooksDetails.DataSource;
    DataTable dt = dsTempDataTable.Tables[0]; // use table index/name to get the exact table
    dt.Rows[0]["columnName"] = "some value";
    // your row edit  code
    dt.AcceptChanges();
    

提交回复
热议问题