我有一个SQL数据库绑定的数据集,它是Datagridview(dgv)的数据源。我想允许用户通过右键单击行头并从上下文菜单条中选择一个选项来复制和粘贴行。我已经做了。
如何复制,编辑然后添加行?
我到目前为止的代码,副本,编辑旧行和新行然后添加?
//Get row
var newrow = JoblistDataSet.Tables["Joblist"].Rows[rowIndex];
//Duplicate row
var copy = newrow;
//Get next id of Identity Column of database
var lastid = getLastID() +1 ;
//Sets the ID column of row to the nextID
copy[0] = lastid;
JoblistDataSet.Tables["Joblist"].ImportRow(copy);
答案 0 :(得分:1)
如果你想从前一行复制整行,这是一种可能的方式
// Row to copy from
DataRow dr = JoblistDataSet.Tables["Joblist"].Rows[rowIndex];
// Row that receives the values from source
DataRow newrow = JoblistDataSet.Tables["Joblist"].NewRow();
// Copy the ItemArray of the source row to the destination row
// Note that this is not a reference copy.
// Internally a new object array is created when you _get_ the ItemArray
newrow.ItemArray = dr.ItemArray;
// Change whateever you need to change
newrow[0] = 99;
// Add the new row into the datatable collection
JoblistDataSet.Tables["Joblist"].Rows.Add(newrow);