时间:2019-05-17 标签:c#objectlistview模型更新未绘制

发布于 2024-10-22 04:53:57 字数 2698 浏览 1 评论 0原文

我正在使用 objectlistview (olv) 来显示依赖于主记录的子属性集。这是使用 linq 的 ac# 应用程序。我不使用 olv 作为行编辑器;相反,我将底层子记录弹出到动态的、更详细的非 wpf 对话框中。如果 olv 列的模型方面依赖于对象(即外键),则 olv 显示不会刷新。

例如,

olv 显示包含状态日志,其中一列是“Status.Code”,它与对象“Log”相关,如下所示:“Log.Status”。因为是linq,所以“Log.StatusId”也存在于linq DataContext中(但未配置为在olv中显示)。 “Log.StatusId”从编辑对话框中正确返回,并且“Log.Status”在编辑对话框终止后立即正确填充。此外,linq 可以正确保存编辑。

我尝试过 olv 的 Invalidate() 和 BuildList() 以及几天的 hoodoo,但都失败了。这是一个普通的 olv - 不是 rapidlistview 或 datalistview。欢迎任何观点。

下面的代码强调了外键的处理。对于非 olv 用户,olv 的配置与大多数其他 Windows 窗体控件一样。

...
object old = DataService.Clone<object>(olv.SelectedObject);
  // where old ~ reference base for changes to olv object - for state management and linq
object row = olv.SelectedObject;
  // where row ~ object that receives edits and undergoes updates via linq
Dictionary<string, object> rowState = new Dictionary<string, object>();
  // where ~ rowState<fieldName,originalValue>
RecordDetail dlg = new RecordDetail(GetUser(), master.GetType(), row, rowState);
  // where ~ GetUser() & master.GetType() configure form RecordDetail
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
    if (row != null && rowState != null && rowState.Count > 0)
    {
        int id = DataService.GetPrimaryKeyValue(row);
        if (id > 0) /// if not new
        {
            int pk = DataService.GetPrimaryKeyValue(old);
            MultiState state = getChildState(olv); // olv.Tag contains state
            foreach (KeyValuePair<string, object> change in rowState)
            {
                MethodInfo mi = old.GetType().GetMethod(DataService.LINQ_GET + change.Key);
                object newValue = mi.Invoke(row, null);
                bool ok = DataService.ManageMultiStateUpdate(ref state, pk, change.Key, newValue, change.Value, old);
                /// INFO populate fk objects for olv // <== works ok
                Type tdomain = DataService.GetForeignKeyTypeAsAliasSafe(old.GetType(), change.Key);
                if (tdomain != null)
                {
                    object fko = GetForeignKey(tdomain, change.Value);
                    mi = row.GetType().GetMethod(DataService.LINQ_SET + change.Key.Replace(DataService.LI_ID, ""));
                    object[] args = { fko };
                    mi.Invoke(row, args);
                }
                ...
             }
             olv.BuildList(); // <== both this and olv.Invalidate() fail to display foreign key updates
             ...
         }
         ...
     }
     ...
 }
 ...

I'm using objectlistview (olv) to display sets of child attributes dependent on a master record. it's a c# app that uses linq. i do not use olv as a row editor; instead, i pop out the underlying child record into a dynamic, more-detailed, non-wpf dialog. if an olv's column's model aspect relies on objects (ie, foreign keys), the olv display does not refresh.

for example,

an olv display contains a log of statuses, and one column is "Status.Code" which relates to the object "Log" like this: "Log.Status". because it is linq, "Log.StatusId" also exists in the linq DataContext (but is not configured to display in the olv). "Log.StatusId" returns correctly from the edit dialog and "Log.Status" populates correctly immediately after edit dialog terminiates. also, linq saves edits correctly.

i've tried and failed with olv's Invalidate() and BuildList() and a few days' worth of hoodoo. this is an ordinary olv - not a rapidlistview or a datalistview. any perspective welcomed.

the code below underscores handling of foreign keys. for non-olv users, olv is configured like most other windows form controls.

...
object old = DataService.Clone<object>(olv.SelectedObject);
  // where old ~ reference base for changes to olv object - for state management and linq
object row = olv.SelectedObject;
  // where row ~ object that receives edits and undergoes updates via linq
Dictionary<string, object> rowState = new Dictionary<string, object>();
  // where ~ rowState<fieldName,originalValue>
RecordDetail dlg = new RecordDetail(GetUser(), master.GetType(), row, rowState);
  // where ~ GetUser() & master.GetType() configure form RecordDetail
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
    if (row != null && rowState != null && rowState.Count > 0)
    {
        int id = DataService.GetPrimaryKeyValue(row);
        if (id > 0) /// if not new
        {
            int pk = DataService.GetPrimaryKeyValue(old);
            MultiState state = getChildState(olv); // olv.Tag contains state
            foreach (KeyValuePair<string, object> change in rowState)
            {
                MethodInfo mi = old.GetType().GetMethod(DataService.LINQ_GET + change.Key);
                object newValue = mi.Invoke(row, null);
                bool ok = DataService.ManageMultiStateUpdate(ref state, pk, change.Key, newValue, change.Value, old);
                /// INFO populate fk objects for olv // <== works ok
                Type tdomain = DataService.GetForeignKeyTypeAsAliasSafe(old.GetType(), change.Key);
                if (tdomain != null)
                {
                    object fko = GetForeignKey(tdomain, change.Value);
                    mi = row.GetType().GetMethod(DataService.LINQ_SET + change.Key.Replace(DataService.LI_ID, ""));
                    object[] args = { fko };
                    mi.Invoke(row, args);
                }
                ...
             }
             olv.BuildList(); // <== both this and olv.Invalidate() fail to display foreign key updates
             ...
         }
         ...
     }
     ...
 }
 ...

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

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

发布评论

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

评论(1

缘字诀 2024-10-29 04:53:57

AddObjects 而不是 AddObject 没有工作。 2008 年 11 月修复也没有补救。我当前唯一的解决方案是清除并重新添加原始对象的编辑列表,并使用 hack 值 id 值进行添加。任何数据源绑定/状态都是无关紧要的,因为记录是不关联的 linq 对象。耶。

AddObjects instead of AddObject did not work. november 2008 fixes also did not remedy. my current, only solution is to clear and re-add an edited list of original objects with a hack value id value for additions. any datasource binding / state is irrelevant since records are disassociated linq objects. yay.

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