无法将行转换为 C# 中的 DataTable

发布于 2024-12-07 10:43:49 字数 1797 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上芳菲 2024-12-14 10:43:49
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)
    {
        //
    }
}
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)
    {
        //
    }
}
季末如歌 2024-12-14 10:43:49
var someRow = newTable.NewRow();
someRow[0] = row[0]; // etc
newTable.Rows.Add(someRow);
var someRow = newTable.NewRow();
someRow[0] = row[0]; // etc
newTable.Rows.Add(someRow);
荒岛晴空 2024-12-14 10:43:49

看起来您正在使用 newtable 作为临时容器,将 dt 中的每一行发送到 GetInfo 方法。如果是这样,为什么不更改 GetInfo 方法以获取 DataRow 而不是包含单个 DataRowDataTable ?然后,您可以摆脱 newtable 而不必费心创建和复制 DataRow

private static void BatchFrontSidePrinting(Student St, frmBaseCard frm) 
{ 
    DBINFOPACK UserInfo ; 
    DBCARDINFO CardInfo; 

    foreach (DataRow row in dt.Rows) 
    { 
        try 
        {       
            // just pass the row
            UserInfo = GetInfo(row); 

            // rest of the code as before
            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)); 
        } 

It looks like you are using newtable as a temporary container to send each row in dt to the GetInfo method. If so, why not change the GetInfo method to take a DataRow rather than a DataTable that contains a single DataRow? Then you can get rid of newtable and not bother with creating and copying DataRows in the first place.

private static void BatchFrontSidePrinting(Student St, frmBaseCard frm) 
{ 
    DBINFOPACK UserInfo ; 
    DBCARDINFO CardInfo; 

    foreach (DataRow row in dt.Rows) 
    { 
        try 
        {       
            // just pass the row
            UserInfo = GetInfo(row); 

            // rest of the code as before
            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)); 
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文