首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Winforms字典DataGrid绑定自动刷新不失去焦点

问C# Winforms字典DataGrid绑定自动刷新不失去焦点
EN

Stack Overflow用户
提问于 2010-07-15 12:38:36
回答 1查看 2.2K关注 0票数 1

设置

  • C# WinForms Application.

摘要

将字典绑定到datagridview.

  • Updating的
  • 会自动更新数据库。当更新发生时,数据集不会失去焦点。
  • 绑定可以双向工作(在网格中编辑值会更新字典。

)

场景

database.

  • The "MyCustomObject".

  • Each I有一个类,它根据来自数据库中的数据的数据计算值,因此计算值也随之发生变化。
  • --这些计算值被添加到自定义对象的属性中,然后添加到字典中(或者,如果存在自定义对象,则更新自定义对象)。“词典”中的
  • 绑定到一个数据集。

这个想法

思想是计算出的值不断变化,从而更新字典,而字典反过来更新与数据集交互的datagrid.

  • Users,需要能够看到这些更改,automatically.

  • It必须确保当网格使用新的自定义对象值更新时,当前选定的行不会失去焦点。

  • 绑定必须双向工作(网格中的编辑值也会更新字典。

)

问题

我如何实现这个自动绑定,从而得到我所需要的需求呢?我已经编写了代码来完成所有的计算,并添加/更新了MyCustomObjects.

  • Examples的字典,如何实现这个数据绑定将是极大的appreciated.

。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-15 13:00:42

为了实现这一点,您的字典需要实现IBindingList,并在发生更改时触发ListChanged事件。下面是IDictionary<TKey, TValue>的一个示例(非常)基本实现,它也实现了IBindingList

代码语言:javascript
复制
public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList
{
    private Dictionary<TKey, TValue> source = new Dictionary<TKey, TValue>();

    void IBindingList.AddIndex(PropertyDescriptor property) { }
    object IBindingList.AddNew() { throw new NotImplementedException(); }
    bool IBindingList.AllowEdit { get { return false; } }
    bool IBindingList.AllowNew { get { return false; } }
    bool IBindingList.AllowRemove { get { return false; } }
    void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) { }
    int IBindingList.Find(PropertyDescriptor property, object key) { throw new NotImplementedException(); }
    bool IBindingList.IsSorted { get { return false; } }
    void IBindingList.RemoveIndex(PropertyDescriptor property) { }
    void IBindingList.RemoveSort() { }
    ListSortDirection IBindingList.SortDirection { get { return ListSortDirection.Ascending; } }
    PropertyDescriptor IBindingList.SortProperty { get { return null; } }
    bool IBindingList.SupportsChangeNotification { get { return true; } }
    bool IBindingList.SupportsSearching { get { return false; } }
    bool IBindingList.SupportsSorting { get { return false; } }
    int System.Collections.IList.Add(object value) { throw new NotImplementedException(); }
    void System.Collections.IList.Clear() { Clear(); }
    bool System.Collections.IList.Contains(object value) { if (value is TKey) { return source.ContainsKey((TKey)value); } else if (value is TValue) { return source.ContainsValue((TValue)value); } return false; }
    int System.Collections.IList.IndexOf(object value) { return -1; }
    void System.Collections.IList.Insert(int index, object value) { throw new NotImplementedException(); }
    bool System.Collections.IList.IsFixedSize { get { return false; } }
    bool System.Collections.IList.IsReadOnly { get { return true; } }
    void System.Collections.IList.Remove(object value) { if (value is TKey) { Remove((TKey)value); } }
    void System.Collections.IList.RemoveAt(int index) { throw new NotImplementedException(); }
    object System.Collections.IList.this[int index] { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }

    private ListChangedEventHandler listChanged;

    event ListChangedEventHandler IBindingList.ListChanged
    {
        add { listChanged += value; }
        remove { listChanged -= value; }
    }

    protected virtual void OnListChanged(ListChangedEventArgs e)
    {
        var evt = listChanged;

        if (evt != null) evt(this, e);
    }

    public void Add(TKey key, TValue value)
    {
        source.Add(key, value);

        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    public bool Remove(TKey key)
    {
        if (source.Remove(key))
        {
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            return true;
        }

        return false;
    }

    public TValue this[TKey key]
    {
        get
        {
            return source[key];
        }
        set
        {
            source[key] = value;

            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
    {
        ((ICollection<KeyValuePair<TKey, TValue>>)source).Add(item);

        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
    {
        if (((ICollection<KeyValuePair<TKey, TValue>>)source).Remove(item))
        {
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            return true;
        }

        return false;
    }

    public bool ContainsKey(TKey key) { return source.ContainsKey(key); }
    public ICollection<TKey> Keys { get { return source.Keys; } }
    public bool TryGetValue(TKey key, out TValue value) { return source.TryGetValue(key, out value); }
    public ICollection<TValue> Values { get { return source.Values; } }
    public void Clear() { source.Clear(); }
    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) { return ((ICollection<KeyValuePair<TKey, TValue>>)source).Contains(item); }
    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { ((ICollection<KeyValuePair<TKey, TValue>>)source).CopyTo(array, arrayIndex); }
    public int Count { get { return source.Count; } }
    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly { get { return ((ICollection<KeyValuePair<TKey, TValue>>)source).IsReadOnly; } }
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return source.GetEnumerator(); }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
    bool ICollection.IsSynchronized { get { return false; } }
    object ICollection.SyncRoot { get { return null; } }
    void ICollection.CopyTo(Array array, int arrayIndex) { ((ICollection)source).CopyTo(array,arrayIndex); }
}

不幸的是,所选的行可能更改,也可能不更改,这取决于所使用的网格。您的最佳选择是在选定行的键发生更改时将其保存下来,然后在字典更新时重新选择该行(如果存在)。否则,就无法保证您将维护所选内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3255547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档