asp.net 将行从一个数据表复制到另一个数据表
我有一个数据表,像这样,我已经根据某些主数据从数据表中搜索了数据行,现在我想将搜索到的行添加到另一个数据表中,我如何才能实现这一点,请
DataTable findRows = (DataTable)ViewState["dt"];
List<int> selectedList=(List<int>)ViewState["selectedList"];
DataTable temp = new DataTable();
foreach (int id in selectedList)
{
DataRow dr=findRows.Rows.Find(id);
}
现在告诉我,我希望将其添加到数据表临时中我怎样才能做到这一点?
i have a datable and like this i have searched a datarow from the datable on the basis of some primary now i want to add that searched row to another datatable how can i achieve this please let me know
DataTable findRows = (DataTable)ViewState["dt"];
List<int> selectedList=(List<int>)ViewState["selectedList"];
DataTable temp = new DataTable();
foreach (int id in selectedList)
{
DataRow dr=findRows.Rows.Find(id);
}
now i want it to add to datatable temp how can i achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,创建
temp
时,不要只是将其实例化为新的DataTable
,而是在findrows
上调用.Clone()
code> 创建结构相同的DataTable
。其次,在第二个
DataTable
上使用.ImportRow()
并将您要复制的第一个DataTable
中的行传递给它。这应该在第二个表中创建一个全新的行,其值与第一个表中的行相同。First, when creating
temp
don't just instantiate it as a newDataTable
but instead call.Clone()
onfindrows
to create a structurally identicalDataTable
.Second, use
.ImportRow()
on the secondDataTable
and pass it the row from the firstDataTable
that you'd like to copy. This should create an entirely new row in the second table with the same values as the row from the first table.