使用 DataColumn 的 Expression 属性从子行获取确认文本



我在两个数据表中有数据集,MasterTable 有指向 DetailTable 的 PK 关系链接。我知道表达可以帮助总结孩子喜欢的价值

dtMaster.Columns.Add(new DataColumn("TotalQty", 0, "Sum(child.Qty)"));

但我需要简洁的字符串,例如

dtMaster.Columns.Add(new DataColumn("HasErrors", 0, "Concat(child.ErrorText + ';')"));

我知道列表达式不支持 concat,所以现在,我需要检测 Table.ColumnChanging 事件以菜单方式设置主节点的 HasError 字段值。

有人可以更自动地引导我这样做吗?

这是我的示例代码

internal static void Test()
{
var ds = new DataSet();
var dtMaster = ds.Tables.Add("Master");
var colIDMaster = dtMaster.Columns.Add("ID");
var dtDetail = ds.Tables.Add("Detail");
var colIDDetail = dtDetail.Columns.Add("ID");
dtDetail.Columns.Add("CID");
dtDetail.Columns.Add("Qty", typeof(int));
dtDetail.Columns.Add("ErrorText", typeof(string), "IIF(Qty is null, 'Qty err', '')");
ds.Relations.Add("m2d", colIDMaster, colIDDetail, false);
//I know I can do this
dtMaster.Columns.Add("TotalQty", typeof(int), "Sum(child.Qty)");
//here is I am asking solution
//dtMaster.Columns.Add("ContainErrors", typeof(string), "Concat(child.ErrorText + ';')");
var newMasterRow = dtMaster.NewRow();
newMasterRow["ID"] = "A";
dtMaster.Rows.Add(newMasterRow);
var newDetailRow = dtDetail.NewRow();
newDetailRow["ID"] = "A";
newDetailRow["CID"] = "D1";
newDetailRow["Qty"] = DBNull.Value;
dtDetail.Rows.Add(newDetailRow);
newDetailRow = dtDetail.NewRow();
newDetailRow["ID"] = "A";
newDetailRow["CID"] = "D2";
newDetailRow["Qty"] = 10;
dtDetail.Rows.Add(newDetailRow);
//now I can loop and print the  master row with ID and it's ErrorTexts
}

感谢您的帮助

您需要在"详细信息"中添加列值更改事件。

dtDetail.ColumnChanged += dtDetail_ColumnChanged;
static void dtDetail_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
if (e.Column.ColumnName == "Qty")
{
int sum = 0;
// dtMaster.Columns.Add("TotalQty"
foreach(DataRow row in ds.Tables["Detail"].Rows)
{
if (row["Qty"] != DBNull.Value && Convert.ToString(row["CID"]) != Convert.ToString(e.Row["CID"]))
{
sum += Convert.ToInt32(row["Qty"]);
}
}
sum += Convert.ToInt32(e.Row["Qty"]);
ds.Tables["Master"].Rows[0]["TotalQty"] = sum;
}
}

相关内容

  • 没有找到相关文章

最新更新