如何在 winforms ADO.net 应用程序中将自定义非 DataTable 列添加到我的 DataView?

发布于 2024-08-18 13:10:21 字数 808 浏览 9 评论 0原文

我如何(/是否可能)向我的 DataView 添加自定义列,并在此列中显示特定计算的结果。

也就是说,我当前有一个 dataGridView,它基于数据库中的 DataTable 绑定到 DataView。我想向 dataGridView 添加一个附加列来显示一个数字,该数字是通过查看当前行及其子行来计算的。换句话说,列的信息不仅仅可以从行数据本身得出。

具体问题可能是: a) 在哪里添加列本身?到我假设的数据视图? b)哪个方法/事件触发此自定义列的值的重新计算(/我如何控制这个)

谢谢

PS。我还注意到,如果我使用以下代码/方法,我会得到无限循环...

    // Custom Items
    DataColumn dc = new DataColumn("OverallSize", typeof(long));
    DT_Webfiles.Columns.Add(dc);
    DT_Webfiles.RowChanged += new DataRowChangeEventHandler(DT_Row_Changed);

private static void DT_Row_Changed(object sender, DataRowChangeEventArgs e)
{
    e.Row["OverallSize"] = e.Row["OverallSize"] ?? 0;
    e.Row["OverallSize"] = (long)e.Row["OverallSize"] + 1;
}

还有什么其他方法可以避免这种循环。即目前我说的是当行更改时更新自定义列的值,但是在更改行之后它会触发另一个“行已更改”事件...

How could I (/is it possible) to add a custom column to my DataView, and in this column display the result of a specific calculation.

That is, I currently have a dataGridView which has a binding to a DataView, based on the DataTable from my database. I'd like to add an additional column to the dataGridView to display a number which is calculated by looking at this current row plus it's children row. In other words the info for the column isn't just derivable from the row data itself.

Specific questions might be:
a) where to add the column itself? to the DataView I assume?
b) which method / event to trigger the re-calculation of the value of this custom column from ( / how do I control this)

Thanks

PS. I've also noted if I use the following code/approach I get a infinite loop...

    // Custom Items
    DataColumn dc = new DataColumn("OverallSize", typeof(long));
    DT_Webfiles.Columns.Add(dc);
    DT_Webfiles.RowChanged += new DataRowChangeEventHandler(DT_Row_Changed);

private static void DT_Row_Changed(object sender, DataRowChangeEventArgs e)
{
    e.Row["OverallSize"] = e.Row["OverallSize"] ?? 0;
    e.Row["OverallSize"] = (long)e.Row["OverallSize"] + 1;
}

What other approach could avoid this looping. i.e. currently I'm saying update the value of the custom column when the row changes, however after then changing the row it triggers antoher 'row has changed' event...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-08-25 13:10:21

我认为这个链接可能是一个很好的答案?虽然我还不确定表达式 DataColumn.Expression 属性是否能够处理它...(即根据 ITEM_TABLE< 为当前项的子项对所有 Item.size 进行求和 >RELATIONSHIP_TABLE 构造。Relationship_table 有一个 ID、ITEM_PARENT_ID、ITEM_CHILD_ID。

== ">http://ondotnet.com/pub/a/dotnet/2003/05/26/datacolumn_expressions.html

I think this link may be a good answer? Although I'm not sure yet whether expression DataColumn.Expression property will be able to handle it yet...(i.e. doing a SUM of all Item.size for all items that are a child of the current one, based on the ITEM_TABLE<==>RELATIONSHIP_TABLE construct. Relationship_table has an ID, ITEM_PARENT_ID, ITEM_CHILD_ID.

http://ondotnet.com/pub/a/dotnet/2003/05/26/datacolumn_expressions.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文