首页 > 解决方案 > 在数据表c#中的下一个位置复制并插入现有行

问题描述

我有一个数据表,需要在原始数据下方复制一行 x 次(由 xMulti 命名)。我怎么能做到这一点?我的例子给出了一个错误。

在此处输入图像描述

int xMultiPos = 0;
if (DtTbl.Columns.Contains("xMulti"))
{
    xMultiPos = DtTbl.Columns.IndexOf("xMulti"); // set the position of the multiplikator
    foreach (DataRow row in DtTbl.Rows)
    {
        int xMultiplicatorTemp = int.Parse((string)row[xMultiPos]); // convert the multiplicator to an int value
        if (xMultiplicatorTemp > 1)
        {
            TB_Info.Text = "vorhanden";
            for (int i = 0; i < xMultiplicatorTemp; i++)
            {
                int index = DtTbl.Rows.IndexOf(row);
                try
                {
                    DataRow temp = (DataRow)row[index]; // this will not work, because it is text?
                    DtTbl.Rows.InsertAt(temp, index);
                }
                catch (Exception msg)
                {
                    Debug.WriteLine(msg);
                    throw;
                }
            }
        }
    }
}

在此处输入图像描述

标签: c#datatablecopy

解决方案


推荐阅读