我正在处理一个包含2个类似数据网格的winform,每个数据源都将其数据源设置为同一个表的不同实例。出于测试目的,表格具有不同的和类似的记录,每个记录都填充不同的单元格。
1-调用Datatable.Merge时,目标表获取缺失的行,但是初始行没有从源表的相应单元格更新其空单元格。
2-在目标表上调用Datatable.RejectChanges时,不会删除合并添加的行,这是一个问题,但这次,初始行的空单元格从相应的单元格中正确更新源表,我希望在合并时发生。
用于填充表格的函数:
private bool OpenFile(Datatable table)
{
if (OFD.ShowDialog() == DialogResult.OK) // OFD is OpenFileDialog
{
if (UserPrompt.ShowDialog() == DialogResult.OK) // UserPrompt is a custom input dialog for user credentials
{
try
{
// code to fill table not shown for readability, works well anyway
table.AcceptChanges(); // otherwise all the records are deleted after RejectChanges, including the initial ones
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Operation Canceled", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
return false;
}
合并功能:
private void menuMerge1_Click(object sender, EventArgs e)
{
Table1.Merge(Table2, true);
if (MessageBox.Show("Table 2 merged to table 1. Save changes? Choosing no will cancel the merging.", "Keep or Discard Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK)
Save(Table1); // saving to file, custom function
else Table1.RejectChanges();
}
首先调用OpenFile函数,然后调用合并,就是它。
我试过了:
提前感谢任何见解!
答案 0 :(得分:0)
好的,首先,DataTable.Merge不会引发事件,这意味着RejectChanges仅适用于已修改的行,添加的行标记为Unchanged。解决方案是编写自定义合并函数。适用于类似的表,假设PK是第一列,单列:
private bool Merge(DataTable target, DataTable source, bool preserve)
{
if (!preserve)
{
target.Merge(source, false);
return true;
}
else
{
DataRow drTargetMatch;
DataRow drNew;
int numCol = source.Columns.Count;
int index = 1;
try
{
foreach (DataRow dr in source.Rows)
{
drTargetMatch = target.Rows.Find(dr[0]);
if (drTargetMatch == null) // source row does not exist in target, add a copy
{
drNew = target.NewRow();
drNew.ItemArray = dr.ItemArray;
target.Rows.Add(drNew);
}
else // source row exists in target, update empty values
{
for (index = 1; index < numCol; index++) // start at 1 since we don't update PK
{
if (drTargetMatch.IsNull(index) && !dr.IsNull(index))
drTargetMatch[index] = dr[index];
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Operation Canceled", MessageBoxButtons.OK, MessageBoxIcon.Error);
target.RejectChanges();
return false;
}
return true;
}
}