在现有数据表中插入一行
Insert a Row in Existing DataTable
我尝试了一个问题的方法。
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;
}
我收到此运行时错误:此行已属于另一个 table。
我尝试了不同的插入方法,但错误是一样的,我也参考了一些帖子,但它似乎不起作用
请帮忙!!!!
改变这个:
var rowTemp = temp.NewRow();
对此:
var rowTemp = curvePoints.NewRow();
事实上,该行必须由您要添加的 table 创建
您的这部分代码:
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);
}
不正确。首先,您要向 temp
添加新行,然后尝试将其插入 curvePoints
。因为它已经属于 temp
数据表 - 你得到了你提到的异常。
这段代码可以简化为
for (j = 0; j < temp.Rows.Count; j++)
curvePoints.ImportRow(temp.Rows[j]);
但请注意:ImportRow
在最后位置插入行,因此如果您真的需要像您正在做的那样将行插入特定位置 - 只需保持代码不变,只更改 var rowTemp = temp.NewRow();
至 var rowTemp = curvePoints.NewRow();
我尝试了一个问题的方法。
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;
}
我收到此运行时错误:此行已属于另一个 table。 我尝试了不同的插入方法,但错误是一样的,我也参考了一些帖子,但它似乎不起作用 请帮忙!!!!
改变这个:
var rowTemp = temp.NewRow();
对此:
var rowTemp = curvePoints.NewRow();
事实上,该行必须由您要添加的 table 创建
您的这部分代码:
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);
}
不正确。首先,您要向 temp
添加新行,然后尝试将其插入 curvePoints
。因为它已经属于 temp
数据表 - 你得到了你提到的异常。
这段代码可以简化为
for (j = 0; j < temp.Rows.Count; j++)
curvePoints.ImportRow(temp.Rows[j]);
但请注意:ImportRow
在最后位置插入行,因此如果您真的需要像您正在做的那样将行插入特定位置 - 只需保持代码不变,只更改 var rowTemp = temp.NewRow();
至 var rowTemp = curvePoints.NewRow();