Reputation: 34028
I am trying to explain what I need to do.
As you can see in the second foreach, I am iterating over the temporary data table, but I need to set a value for the same row in the original data tablerow. For example: _uc090_WingsIntegrationDataSet.WingsBookingInterface[0]["property"] = x;
What I dont know how to implement is how to find that row and set the property, I saw the LoadRow method but I never used it before.
DataTable tempTable = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Clone();
DataRow[] datarows = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Select("REFMDossierID = " + refmDossierId);
if (datarows.Length > 0)
{
foreach (DataRow dr in datarows)
{
tempTable.ImportRow(dr);
}
}
//2. foreach master row
foreach (UC090_WingsIntegrationDataSet.WingsBookingInterfaceRow row in tempTable.Rows)
Upvotes: 0
Views: 8868
Reputation: 13141
You can find the row using Rows.Find()
, but it requires that a PrimaryKey be set on at least one column in your DataTable.
As far as loading new data, you can use LoadDataRow()
which will update existing rows (if a primary key is supplied) or insert new data if any matching datatypes are found.
Please take a look at the following example using untyped datasets:
DataSet dataSet = new DataSet("MyDataSet");
DataTable dataTable = dataSet.Tables.Add("JavaScriptLibraries");
DataColumn[] dataColumns =
new[] {
new DataColumn("Id", typeof(Int32))
{
AutoIncrement = true,
AllowDBNull = false,
AutoIncrementSeed = 1
},
new DataColumn("Name", typeof(String))
};
dataTable.Columns.AddRange(dataColumns);
dataTable.PrimaryKey = new[] { dataTable.Columns["Id"] };
DataRow dataRow1 = dataTable.NewRow();
dataRow1["Name"] = "jQuery";
dataTable.Rows.Add(dataRow1);
DataRow dataRow2 = dataTable.NewRow();
dataRow2["Name"] = "MooTools";
dataTable.Rows.Add(dataRow2);
// Copy the dataset
DataSet tempDataSet = dataSet.Clone();
DataTable tempDataTable = tempDataSet.Tables["JavaScriptLibraries"];
DataRow[] tempRows = dataSet.Tables["JavaScriptLibraries"].Select("Name = 'jQuery'");
// Import rows to copy of table
foreach (var tempRow in tempRows)
{
tempDataTable.ImportRow(tempRow);
}
foreach (DataRow tempRow in tempDataTable.Rows)
{
// Find existing row by PK, then update it
DataRow originalRow = dataTable.Rows.Find(tempRow["Id"]);
originalRow["Name"] = "Updated Name";
}
// Load new data using LoadDataRow()
object[] newRow = new[] { null, "New Row" };
dataTable.BeginLoadData();
dataTable.LoadDataRow(newRow, true);
dataTable.EndLoadData();
Upvotes: 2