Dear all,
I met the problem with the following codes. In my scenario, the application has to create the new dataTable in the dataSet, then hook the two tables by using the relation. E.g. Table["p"] is the child table of Table["s"].
dataSet.Clear();
// if either table s or p is missing, add the new one.
if (dataSet.Tables["s"] == null
|| dataSet.Tables["p"] == null)
{
DataTable dtS = new DataTable("s");
dtS.Columns.Add("m");
dtS.Columns["m"].ColumnMapping = MappingType.Attribute;
dtS.Columns.Add("s_id", typeof(int));
dtS.Columns["s_id"].ColumnMapping = MappingType.Hidden;
dtS.Rows.Add("B", 0);
dataSet.Tables.Add(dtS);
/// p
DataTable dtP = new DataTable("p");
dtP.Columns.Add("s_id", typeof(int));
dtP.Columns["s_id"].ColumnMapping = MappingType.Hidden;
dtP.Columns.Add("i");
dtP.Columns.Add("s");
dtP.Columns.Add("a");
dtP.Columns.Add("p");
dtP.Columns.Add("g");
dataSet.Tables.Add(dtP);
dataSet.Relations.Add("s_p", dtS.Columns["s_id"], dtP.Columns["s_id"]);
dataSet.Relations["s_p"].Nested = true;
}
The code for the "paste" action is as follows,
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int row = dgv.CurrentCell.RowIndex;
foreach (string line in lines)
{
dataSet.Tables["p"].Rows.InsertAt(dataSet.Tables["p"].NewRow(), row);
if (row < dgv.RowCount && line.Length > 0)
{
string[] cells = line.Split('\t');
for (int i = 1; i < cells.Length; ++i)
{
dataSet.Tables["p"].Rows[row][i-1] =
Convert.ChangeType(cells
, dgv[i - 1, row].ValueType);
}
row++;
}
else
{
break;
}
}
When I tried above codes, the error "ForeignKeyConstraint s_p requires the child key values (2) to exist in the parent table" is returned at the point
dataSet.Tables["p"].Rows[row][i-1] =
Convert.ChangeType(cellsIdea, dgv[i - 1, row].ValueType);
Can you help me a little with it?
Thanks in advance,
Rush hour
I met the problem with the following codes. In my scenario, the application has to create the new dataTable in the dataSet, then hook the two tables by using the relation. E.g. Table["p"] is the child table of Table["s"].
dataSet.Clear();
// if either table s or p is missing, add the new one.
if (dataSet.Tables["s"] == null
|| dataSet.Tables["p"] == null)
{
DataTable dtS = new DataTable("s");
dtS.Columns.Add("m");
dtS.Columns["m"].ColumnMapping = MappingType.Attribute;
dtS.Columns.Add("s_id", typeof(int));
dtS.Columns["s_id"].ColumnMapping = MappingType.Hidden;
dtS.Rows.Add("B", 0);
dataSet.Tables.Add(dtS);
/// p
DataTable dtP = new DataTable("p");
dtP.Columns.Add("s_id", typeof(int));
dtP.Columns["s_id"].ColumnMapping = MappingType.Hidden;
dtP.Columns.Add("i");
dtP.Columns.Add("s");
dtP.Columns.Add("a");
dtP.Columns.Add("p");
dtP.Columns.Add("g");
dataSet.Tables.Add(dtP);
dataSet.Relations.Add("s_p", dtS.Columns["s_id"], dtP.Columns["s_id"]);
dataSet.Relations["s_p"].Nested = true;
}
The code for the "paste" action is as follows,
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int row = dgv.CurrentCell.RowIndex;
foreach (string line in lines)
{
dataSet.Tables["p"].Rows.InsertAt(dataSet.Tables["p"].NewRow(), row);
if (row < dgv.RowCount && line.Length > 0)
{
string[] cells = line.Split('\t');
for (int i = 1; i < cells.Length; ++i)
{
dataSet.Tables["p"].Rows[row][i-1] =
Convert.ChangeType(cells
}
row++;
}
else
{
break;
}
}
When I tried above codes, the error "ForeignKeyConstraint s_p requires the child key values (2) to exist in the parent table" is returned at the point
dataSet.Tables["p"].Rows[row][i-1] =
Convert.ChangeType(cellsIdea, dgv[i - 1, row].ValueType);
Can you help me a little with it?
Thanks in advance,
Rush hour