无法将行转换为 C# 中的 DataTable
我正在尝试将 DataRow
转换为 DataTable
,但出现错误。我搜索并尝试了所有可能的解决方案,但没有一个有效!
我有一个方法接受 DataTable 作为其参数(此 DataTable 恰好有一行)。该方法将返回一些信息。
起初,我尝试使用 ImportRow(newtable.ImportRow(row))
将 DataRow
转换为 DataTable
,但是 newtable< /code> 之后为空。然后,我尝试了 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));
}
}
}
I'm trying to convert a DataRow
to a DataTable
, but I'm getting errors. I searched and tried all possible solutions, but none worked!
I have a method which accepts a DataTable
as its parameter (this DataTable
has one row exactly). That method will return some information.
At first, I tried to convert the DataRow
to a DataTable
using ImportRow(newtable.ImportRow(row))
, but newtable
is empty afterward. Then, I tried dt.clone()
, but this fills the newtable
with just everything, which is not what I was after! Actually the exact thing I was after.
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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在使用
newtable
作为临时容器,将dt
中的每一行发送到GetInfo
方法。如果是这样,为什么不更改GetInfo
方法以获取DataRow
而不是包含单个DataRow
的DataTable
?然后,您可以摆脱newtable
而不必费心创建和复制DataRow
。It looks like you are using
newtable
as a temporary container to send each row indt
to theGetInfo
method. If so, why not change theGetInfo
method to take aDataRow
rather than aDataTable
that contains a singleDataRow
? Then you can get rid ofnewtable
and not bother with creating and copyingDataRow
s in the first place.