Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 925813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:34:47+00:00 2026-05-15T19:34:47+00:00

Setup C# WinForms Application. Summary Binding a dictionary to a datagridview. Updating the dictionary

  • 0

Setup

  • C# WinForms Application.

Summary

  • Binding a dictionary to a datagridview.
  • Updating the dictionary automatically updates the datagrid.
  • The datagrid does not lose focus when the update happens.
  • The binding works both ways (editing values in the grid updates the dictionary.

Scenario

  • I have a class that calculates values
    based on data from a database.
  • The data in this database constantly changes so hence, the calculated values change too.
  • These calculated values are added to the properties of a custom object "MyCustomObject".
  • Each "MyCustomObject" is then added to a dictionary (or the custom object is updated if it already exists).
  • The Dictionary is bound to a datagrid.

The idea

  • The idea is that the calculated vales continually change and thus update the dictionary which in turn updates the datagrid.
  • Users who are interacting with the datagrid need to be able to see these changes automatically.
  • It is imperative that the currently selected row in the datagrid does not lose focus when the grid is updated with the new custom object values.
  • The binding must work both ways (editing values in the grid updates the dictionary too.

Question

  • How can I implement this automatic binding so that I get the requirements that I need?
  • I have already written the code to do all of the calculations and add/update the dictionary of MyCustomObjects.
  • Examples of how to implement this datagrid binding would be greatly appreciated.
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:34:48+00:00Added an answer on May 15, 2026 at 7:34 pm

    In order to accomplish this, your Dictionary would need to implement IBindingList and fire the ListChanged event whenever changes were made. Here’s a sample (very) barebones implementation of IDictionary<TKey, TValue> that also implements IBindingList:

    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); }
    }
    

    Unfortunately, the selected row may or may not change, depending on which grid you’re using. Your best bet is to save off the key of the selected row whenever it changes, then reselect that row (if present) when the dictionary is updated. Otherwise, there’s no way to guarantee that you’ll maintain the selection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application built using Visual Studio 2008. I added a Setup
Setup: I'm creating a .NET WinForms application in C# to allow our technical support
Does anyone know how to specifically include a winforms setup project to be included
I have a setup project whose primary output is from a Winforms based application.
I'm writing a new .Net 3.5 Winforms single user application and I'm not sure
I am working on a WinForms app in C#, VS2008. How do I setup
Background: Professional tools developer. SQL/DB amateur. Setup: .Net 3.5 winforms app talking to MS
I'm having trouble understanding how to close an C# .NET winforms application. What I'm
I have a .NET WinForms application that I've converted into a COM dll using
I have a WinForms application, which I need to support in multiple languages. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.