I tried a method for the question Asked.
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;
}
I get This runTime Error: This row already belongs to another table.
I have tried different methods to Insert yet the error is same, i referred some posts also but it doesnt seem to work
Please help!!!!
Change this:
var rowTemp = temp.NewRow();
to this:
var rowTemp = curvePoints.NewRow();
In fact the row must be created by the same table you want to add it
This part of your code:
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);
}
is incorrect. First you're adding new row to temp, and then trying to insert it into curvePoints. Since it already belongs to temp datatable - you're getting exception you've mentioned.
This code can be simplified as
for (j = 0; j < temp.Rows.Count; j++)
curvePoints.ImportRow(temp.Rows[j]);
But note: ImportRow inserts row at last position, so if you really need to insert row into specific position like you're doing - just leave your code as is and only change var rowTemp = temp.NewRow(); to var rowTemp = curvePoints.NewRow();
Related
I'm having problem with the population of 2D Charts(polar plot/chart) from the same datagridview. Assuming I have a datagridview filled with data og vant to make three individual plots of this data. The datagridview consists of 15 rows and 4 columns. I need to make a plot for each time the "Theta" column changes, with the x and y values consisting of the two last columns "Phi" and "Ampl(dB)".
I get the out of range error...
Code sample (the logic is inside of a button click):
int rowcount = dataGridView3.RowCount - 1;
double x, y, x2, y2, x3, y3;
sizeTheta = (180 / degreeToInt) + 1;
sizePhi = (360 / degreeToInt2) + 1;
int rowCounter = sizeTheta * sizePhi;
//XY
for (int i = 0; i <= 4; i++)
{
x = Convert.ToDouble(dataGridView3.Rows[i].Cells[2].Value);
y = Convert.ToDouble(dataGridView3.Rows[i].Cells[3].Value);
XY_Plot.Series["XY"].Points.AddXY(x, y);
}
//XZ
for (int i = 5; i <= 9; i++)
{
x2 = Convert.ToDouble(dataGridView3.Rows[i].Cells[2].Value);
y2 = Convert.ToDouble(dataGridView3.Rows[i].Cells[3].Value);
XZ_Plot.Series["XZ"].Points.AddXY(x2, y2);
}
//YZ
for (int i = 10; i <= 14; i++)
{
x3 = Convert.ToDouble(dataGridView3.Rows[i].Cells[2].Value);
y3 = Convert.ToDouble(dataGridView3.Rows[i].Cells[3].Value);
YZ_Plot.Series["YZ"].Points.AddXY(x3, y3);
}
for (int i = 0; i <= x; i++)
{
for (int k = 0; k <= TimeSub; k++)
{
dataGridView1.Rows.Add(FromDate); // hour
dataGridView1.Rows[k].Cells[1].Value = FromTime;
FromTime = FromTime + 1;
}
FromDate=FromDate.AddDays(1);
}
While I am executing this statement all the rows are filled correctly but the cells are not. Kindly help on this.
You are always looping from the first row in the second loop.
Rows.Add retruns the new row index.
Try this instead:
var rowIndex = dataGridView1.Rows.Add(FromDate); // hour
dataGridView1.Rows[rowIndex].Cells[1].Value = FromTime;
I am trying to run an insert statement into a DataTable (I'm doing it this way so that all functions in the statement will be executed so I can get the intended values)
private void insertintoDT(DataTable destination, string InsertStatement)
{
SqlCommand comm = new SqlCommand(InsertStatement);
SqlDataReader reader = new SqlDataReader();
destination.Load(reader);
}
}
This DataTable will then be loaded into a DataGrid so I can see the values that will be inserted. I tried breaking up the insert statement like so:
private void breakupInsert(String insert)
{
DataTable dt = new DataTable();
dgvInsert.Rows.Clear();
string stemp = insert;
int len = stemp.Length;
int itemp = 0;
stemp = stemp.Remove(0, 12);
itemp = stemp.IndexOf("VALUES") - 1;
gvtable = stemp.Substring(0, itemp);
stemp = stemp.Remove(0, itemp + 9);
int itemcount = stemp.Count(x => x == ',') + 1;
string[] values = new string[itemcount - 1];
//populate values
int h = 0;//Used as a start index
int itemc = 0; //item index
for (int k = 0; k < stemp.Length; k++)
{
if (k == stemp.Length - 2)
{
values[itemc] = ExecuteSQL(stemp.Substring(h, (k + 1) - h));
break;
}
else
if (stemp.Substring(k, 2) == (", "))
{
itemp = stemp.IndexOf(", ");
values[itemc] = ExecuteSQL(stemp.Substring(h, k - h));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
else if (stemp.Substring(k, 2) == ("),"))
{
itemp = stemp.IndexOf("),");
values[itemc] = ExecuteSQL(stemp.Substring(h, k - h));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
else if (stemp.Substring(k, 2) == ("',"))
{
itemp = stemp.IndexOf("',");
values[itemc] = ExecuteSQL(stemp.Substring(h, (k - h) + 1));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
}
for (int j = 0; j < values.Length; j++)
{
this.dgvInsert.Rows.Insert(j, j + 1, values[j], values[j].Length);
}
}
However this breaks when a function is in the insert statement ( cast for example)
Is there a way to do it or is this a lost cause?
To Clarify:
I'm essentially trying to provide the user with a text box that they can paste an SQL statement into, this statement will then be reflected in a datagrid. So if an insert statement is put in, the datatable will reflect the values that are being inserted. But without ever inserting them into the database.
Queries are database-specific, so parsing queries is done by a database-specific component, probably on the database server and otherwise the ADO.NET driver for that database.
You could use SQLite to create an in-memory database, so your end users won't have to set up a separate database (it does require SQLite dll's shipped with your application), however using that approach forces you to use the SQLite dialect, and some queries might not work if you attempt to use them on a different database. It does sound to me like the easiest way out though.
This answer suggests using the entity framework to parse queries, but I don't know if it can help you update the datatable. I haven't tried this myself but it might be worth looking into.
I tried with this code..
for (int i = 0; i < datagridItemEntry.RowCount; i++)
{
int a = Convert.ToInt32(datagridItemEntry.Rows[i].Cells[4].Value);
int b = Convert.ToInt32(datagridItemEntry.Rows[i].Cells[5].Value);
int c = a * b;
datagridItemEntry.SelectedRows[i].Cells[6].Value = c.ToString();
}
I want value of cell 4 & 5 to be get multiplied and the result should be reflect in cell 6..
Nothing is happening with the above code..
Help me with the proper code..
Your problem may be here
datagridItemEntry.SelectedRows[i].Cells[6].Value = c.ToString();
Replace .SelectedRows to .Rows
for (int i = 0; i < datagridItemEntry.RowCount; i++)
{
int a = Convert.ToInt32(datagridItemEntry.Rows[i].Cells[4].Value);
int b = Convert.ToInt32(datagridItemEntry.Rows[i].Cells[5].Value);
int c = a * b;
datagridItemEntry.Rows[i].Cells[6].Value = c.ToString();
}
I want to update all columns one by one in a datatable using a foreach loop. The code below is what I have so far. But it does not seem to work. Your help will be much appreciated.
foreach (DataRow row in myTable.Rows)
{
Double i;
Double j = Convert.ToDouble(row["x"]);
int y = 1;
int aan = (int)row["year"];
if(y == aan)
{
i = j + 2;
}
row["x"]=i;
row.EndEdit();
myTable.AcceptChanges();
}
The code works fine for me, except for a few tweaks. The code is given below:
foreach (DataRow row in myTable.Rows)
{
Double i = 0;
Double j = Convert.ToDouble(row["x"]);
int y = 1;
int aan = Convert.ToInt32(row["year"]);
if(y == aan)
{
i = j + 2;
}
row["x"]=i;
row.EndEdit();
myTable.AcceptChanges();
}
Are you facing any specific issues?