我正在尝试将 DataRow
转换为 DataTable
,但出现错误。我搜索并尝试了所有可能的解决方案,但没有一个奏效!
我有一个方法接受一个DataTable
作为它的参数(这个DataTable
只有一行)。该方法将返回一些信息。
起初,我尝试使用 ImportRow(newtable.ImportRow(row))
将 DataRow
转换为 DataTable
,但是 newtable
之后是空的。然后,我尝试了 dt.clone()
,但这会用所有内容填充 newtable
,这不是我想要的!实际上正是我所追求的。
private static void BatchFrontSidePrinting(Student St, frmBaseCard frm)
{
DBINFOPACK UserInfo;
DBCARDINFO CardInfo;
DataTable newtable = new DataTable("newtable");
foreach (DataRow row in dt.Rows)
{
try
{
// here, I'm trying to send one DataRow as a DataTable to the GetInfo() method,
// and for the next iteratio , after getting the info I'm removing the row which was just added,
// so that for the next iteration, newdatatable is empty. All of the proceeding actions fail !:(
newtable.ImportRow(row); // doesn't work!
UserInfo = GetInfo(newtable);
newtable.Rows.Remove(row); // doesn't work!
St = UserInfo.STUDENT;
((frmFrontSideCard)frm).Replica = UserInfo.CARDINFO.Replica;
if (UserInfo.CARDINFO.Replica)
{
Loger.Items.Add("Replication !");
}
// print
((frmFrontSideCard)frm).Print = St;
// update
CardInfo = UserInfo.CARDINFO;
CardInfo.SID = UserInfo.STUDENT.ID;
CardInfo.BIMAGE = UserInfo.BIMAGE;
SetInfo(CardInfo);
}
catch (Exception exep)
{
Loger.Items.Add(String.Format("Inside [BatchFrontSidePrinting()] : Student {0} {1}:", St.ID, exep.Message));
}
}
}
最佳答案
foreach (DataRow row in dt.Rows)
{
try
{
DataTable newtable = new DataTable();
newtable = dt.Clone(); // Use Clone method to copy the table structure (Schema).
newtable.ImportRow(row); // Use the ImportRow method to copy from dt table to its clone.
UserInfo = GetInfo(newtable);
catch (Exception exep)
{
//
}
}
关于c# - 无法在 C# 中将行转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621464/