我有一个datagridview,我想创建当前行的相同副本,将其显示在datagridview中并将其保存到数据表中。我得到了一切工作,除了.AcceptChanges()没有将数据保存到表中。该行显示在datagridview上就好了。我不知道自己错过了什么。
DataRowView currentDataRowView = (DataRowView)sM_txdetailDataGridView.CurrentRow.DataBoundItem;
DataRow row = currentDataRowView.Row;
DataRow newRow = rDataSet1.SM_txdetail.NewRow();
newRow.ItemArray = row.ItemArray.ToArray();
newRow["Description"] = "New Row";
rDataSet1.SM_txdetail.Rows.Add(newRow);
rDataSet1.SM_txdetail.AcceptChanges();
答案 0 :(得分:1)
我不确定.AcceptChanges()会做什么,但在插入新行时,您应该使用DataTable
将其添加到SqlDataAdapter
。在这种情况下,我使用存储过程进行插入,但您可以根据自己的喜好进行更改:
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataTable dataTable = new DataTable();
DataRow dataRow = dataTable.NewRow();
insertRecord = new SqlCommand(proc, connectString.UConnect);
insertRecord.CommandType = CommandType.StoredProcedure;
//this collects the paramters, data type, and column length
for (int x = 0; x < columnName.Length; x++)
insertRecord.Parameters.Add("@" + columnName[x], SqlDbType.VarChar, 0, param[x]).Value = value[x];
//this collects the rows to be added
for (int x = 0; x < columnName.Length; x++)
dataRow[columnName[x]] = value[x];
dataTable.Rows.Add(dataRow);
dataAdapter.InsertCommand = insertRecord;
dataAdapater.Update(dataTable);