C# 在现有数据表中插入一行

C# 在现有数据表中插入一行,c#,datatable,C#,Datatable,我试着用一种方法回答这个问题 public DataTable allSupportPoints(DataTable createPath, double rMax, double interval, int curveType) { List <DataTable> theCornerCurves = cornerCurves(createPath, rMax, interval, curveType); DataTable curvePoi

我试着用一种方法回答这个问题

public DataTable allSupportPoints(DataTable createPath, double rMax, double interval, int curveType)
    {
        List <DataTable> theCornerCurves = cornerCurves(createPath, rMax, interval, curveType);
        DataTable curvePoints = CustomMerge(theCornerCurves);

        double X1 = Convert.ToDouble(createPath.Rows[0][1]);
        double Y1 = Convert.ToDouble(createPath.Rows[0][2]);
        double X2, Y2;
        int count = curvePoints.Rows.Count;
        double theDistance;
        int pointInsert;
        for (int i = 0; i < count;)
        {
            X2 = Convert.ToDouble(theCornerCurves[i].Rows[0][0]);
            Y2 = Convert.ToDouble(theCornerCurves[i].Rows[0][0]);
            theDistance = distance(X1,Y1,X2,Y2);
            int j=0;
            if ( theDistance> interval)
            {
                pointInsert = Convert.ToInt32 (theDistance / interval);
                DataTable temp = straightLineGenerator(X1, Y1, X2, Y2,interval);
                for (j = 0; j < temp.Rows.Count; j++)
                {
                    var rowTemp = temp.NewRow(); 
                    rowTemp.ItemArray =    temp.Rows[j].ItemArray.Clone() as object[];
                    curvePoints.Rows.InsertAt(rowTemp, i + j);
                }
            }
            X1=Convert.ToDouble(curvePoints.Rows[i+j][0]);
            Y1 = 0;
            count = curvePoints.Rows.Count;
            i = i + 1;
        }
        return curvePoints;

    }
公共DataTable allSupportPoints(DataTable createPath、双rMax、双间隔、int curveType)
{
列出cornerCurves=cornerCurves(创建路径、rMax、间隔、曲线类型);
DataTable curvePoints=自定义合并(CornerCurves);
double X1=Convert.ToDouble(createPath.Rows[0][1]);
double Y1=Convert.ToDouble(createPath.Rows[0][2]);
双X2,Y2;
int count=curvePoints.Rows.count;
距离加倍;
int pointInsert;
对于(int i=0;i间隔)
{
pointInsert=Convert.ToInt32(距离/间隔);
数据表温度=直线发电机(X1,Y1,X2,Y2,间隔);
对于(j=0;j
我得到这个运行时错误:这一行已经属于另一个表。 我尝试了不同的插入方法,但错误是相同的,我也引用了一些帖子,但似乎不起作用 请帮忙

改变这一点:

var rowTemp = temp.NewRow(); 
为此:

var rowTemp = curvePoints.NewRow(); 

事实上,行必须由要添加它的同一个表创建

这部分代码:

for (j = 0; j < temp.Rows.Count; j++)
{
    var rowTemp = temp.NewRow(); 
    rowTemp.ItemArray = temp.Rows[j].ItemArray.Clone() as object[];
    curvePoints.Rows.InsertAt(rowTemp, i + j);
}

但请注意:
ImportRow
在最后一个位置插入行,所以如果您真的需要像现在这样将行插入到特定位置,只需保持代码不变,只需更改
var rowTemp=temp.NewRow()
to
var rowTemp=curvePoints.NewRow()

导入行不会控制插入行的位置,它会像队列(FIFO)一样添加插入行。我想在特定位置插入它。因此InsertAt,但我仍然无法理解它
to
var rowTemp=curvePoints.NewRow()解决了您的问题?不,它没有,它会抛出相同的错误。“System.Data.dll中发生了类型为'System.ArgumentException'的未处理异常。其他信息:此行已属于另一个表。”我找到了解决方案。你一直都是对的。我误解了你的答案。谢谢:)在你的声望低于15之前,你不能投票。您只能将答案标记为已接受(投票向下箭头下方的绿色标记)。线程通常只有在被认为对其他人无用的情况下才会关闭,因此没有必要关闭线程。
for (j = 0; j < temp.Rows.Count; j++)
    curvePoints.ImportRow(temp.Rows[j]);