Clone/Copy,编辑然后添加数据集行
Clone/Copy, Edit then Add Dataset Row
背景
我有一个 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);
如果你想从前一行复制整行,这是一种可行的方法
// 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);
背景
我有一个 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);
如果你想从前一行复制整行,这是一种可行的方法
// 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);