none
How do I refresh a DataGridView based on a strongly typed List<>? RRS feed

  • Question

  • Hi All,

    I have a DataGridView on a Windows Form that has a data source of a strongly typed List<>.

    I add values to this List<> object as I go along, and values on this list should then automatically be displayed on my DataGridView.

    When I add a new item to the List<>, I am currently attempting to do the following:

    dgvResults.EndEdit();
    dgvResults.DataSource = lResults;

    ...but this doesn't work. Or, more accurately, it works the first time, but subsequent additions to the List<>, which also go across the two lines above, do not display newly added List<> items on the DataGridView.

    Can anyone help me work out how I refresh the DataGridView when new items are added to the List<>?

    Many thanks,

    pt
    Sunday, March 29, 2009 12:40 AM

Answers

  • Hello pt,

    If the objective is letting the datagridview receive notification when the datasource's collection or items chagnes, we need to use the BindingList<T> type instead of List<T> type.

    See the following MSDN article to get detailed information,

    http://msdn.microsoft.com/en-us/library/aa480736.aspx

    Please let me know if you need future help on this.


    Thanks,
    Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Tuesday, March 31, 2009 11:52 AM
    Moderator

All replies

  • I seem to have answered my own question, but I'm curious to know if there's a better way of achieving this?

    In essence, it appears that when I add a new item to my List<>, if I set the DataSource of the DataGridView to null, then set it to my List<>, it refreshes the DataGridView.

    Can anyone tell me if there is a better way to get the DataGridView to update with new entries added to my List<>?

    Many thanks,

    pt
    Sunday, March 29, 2009 1:06 AM
  • You can call the Invalidate method of the DataGridView.
    Sunday, March 29, 2009 1:48 AM
  • What is it a list of? You have a class defined, and it is a list<t> whatever the class is? Or is the list<t> a class of itself as well, with overrides for adding and removing?

    RobinS.
    GoldMail.com


    Ts'i mahnu uterna ot twan ot geifur hingts uto.
    Monday, March 30, 2009 7:52 AM
  • Hello pt,

    If the objective is letting the datagridview receive notification when the datasource's collection or items chagnes, we need to use the BindingList<T> type instead of List<T> type.

    See the following MSDN article to get detailed information,

    http://msdn.microsoft.com/en-us/library/aa480736.aspx

    Please let me know if you need future help on this.


    Thanks,
    Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Tuesday, March 31, 2009 11:52 AM
    Moderator