Refresh a ListView

15 posts, 1 answers
  1. Robert
    Robert avatar
    10 posts
    Member since:
    Apr 2012

    Posted 25 Apr 2012 Link to this post

    Hi there
    I want to know how could we refresh a ListView after editing it's items programmatically
    I tried these but no change in the ListView until I grab the data and fill it again after an event.

    I edited just a piece of data

    ListViewDataItem item = lv.Items[pos];               
    item["Variety"] = this[id] ;
     
    lv.Update();
    lv.Refresh();

    Shouldn't it work ?

    I didn't bound it to anything, and it's filled from a collection
    if the strategy isn't the preferable approach please give advices on this
    thanks in advance
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 26 Apr 2012 Link to this post

    Hello Robert, 

    Here's an example. It assumes that you have a RadButton and a RadListControl on a RadForm. 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : RadForm
        {
            List<Item> _items = new List<Item>();
     
            public Form1()
            {
                InitializeComponent();
     
                _items.Add(new Item(1, "Item1"));
                _items.Add(new Item(1, "Item2"));
                _items.Add(new Item(1, "Item3"));
                _items.Add(new Item(1, "Item4"));
     
                this.radListView1.DataSource = _items;
                this.radListView1.DisplayMember = "Name";
                this.radListView1.ValueMember = "Id";
            }
     
            private void radButton1_Click(object sender, EventArgs e)
            {
                this.radListView1.BeginUpdate();
                int i = 1;
                foreach (Item item in _items)
                {
                    item.Name = "item" + i.ToString() + " updated " + DateTime.Now.ToLongTimeString();
                    i++;
                }
                this.radListView1.EndUpdate();
            }
     
     
        }
     
        public class Item
        {
            public Item()
            { }
     
            public Item(int id, string name)
            {
                Id = id;
                Name = name;
            }
     
            public int Id
            { get; set; }
     
            public string Name
            { get; set; }
        }
    }

    Hope this helps. 
    Please remember to mark as answer if this solves your problem, or let me know if you need further help
    Richard
  3. Robert
    Robert avatar
    10 posts
    Member since:
    Apr 2012

    Posted 26 Apr 2012 Link to this post

    Thanks Richard
    but What I have asked was a bit different
    imagine you have thousands of record in a list view
    After a click you want to update just one of the record's fields
    for example it's "price"
    Should you Clear and repopulate all the data in the ListView from your collection ?!
    Isn't there any other shorthand doing this ?
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 27 Apr 2012 Link to this post

    Hello Robert, 

    I think what you have asked, and the sample express the same thing as far as I can tell. Wrap your statement to update your record with 
    this.radListView1.BeginUpdate();
    // update the one item
    this.radListView1.EndUpdate();
     and you should be fine. If this is not the case, please provide a full sample so I can help you further
    Thanks
    Richard
  5. Robert
    Robert avatar
    10 posts
    Member since:
    Apr 2012

    Posted 28 Apr 2012 Link to this post

    Thanks again,
    In my code, I did this,
    I was thinking maybe I'm wrong or there should be another way also,
    anyway it helped me understanding the scenario
  6. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 30 Apr 2012 Link to this post

    Hi Robert,

    Thank you for writing.

    You can assign a value of an item by using your code and the controls will be automatically updated. Here is a sample:
    RadListView lv = new RadListView();
     
    public Form1()
    {
        InitializeComponent();
         
        this.Controls.Add(lv);
        lv.Size = new System.Drawing.Size(500, 500);
        lv.Columns.Add("Column1");
        lv.Columns.Add("Column2");
        lv.Items.Add("cell 1", "cell 2");
        lv.Items.Add("cell 1", "cell 2");
        lv.Items.Add("cell 1", "cell 2");
        lv.ViewType = ListViewType.DetailsView;
    }
     
     void radButton1_Click(object sender, EventArgs e)
    {
        lv.Items[2]["Column1"] = "hello";
    }

    However, when you are performing batch operations it is better to execute them in BeginUpdate/EndUpdate block in order to increase the performance.

    I hope this helps. 
     
    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  7. Jordan
    Jordan avatar
    3 posts
    Member since:
    Mar 2013

    Posted 15 Jun 2016 in reply to Stefan Link to this post

    I am with Robert on this one. This is a consistent problem with Telerik controls. if you update values on the databounditem, it does not trigger a visualitemformatting. Even if you rebind the item, it still doesn't trigger it, and you basically have to find creative ways to get the item to repaint; toggle the Dock state, toggle Visible, etc., depending upon the control in question. The methods on the control don't work, like Refresh. The only way to consistently to get it to update is to manually scroll the control.

    using BeginUpdate & EndUpdate does not work. there needs to be a better way to repaint an item. What's the point of having a list of bound items, if when those bound items change, it doesn't reflect in the control?

  8. Hristo
    Admin
    Hristo avatar
    1512 posts

    Posted 16 Jun 2016 Link to this post

    Hi Jordan,

    Thank you for writing.

    Each control has its own specifics in terms of data binding and layout updates. The collection to which you bind is also an important factor and in certain cases, the model objects would need to implement the INotifyPropertyChanged interface.

    Regarding RadListView, if you directly update any of the data objects and you need to manually trigger an update in the control you can do it as simple as this: 
    this.radListView1.ListViewElement.SynchronizeVisualItems();

    In case you need additional information about particular controls, I encourage you to open up a support ticket or a new forum thread where we could discuss your exact scenario.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  9. Jordan
    Jordan avatar
    3 posts
    Member since:
    Mar 2013

    Posted 16 Jun 2016 in reply to Hristo Link to this post

    thanks for getting back to me on this. The SynchronizeVisualItems method did the trick.
  10. Gerry
    Gerry avatar
    34 posts
    Member since:
    Jan 2017

    Posted 27 Feb 2017 Link to this post

    It would be nice to see this updated. RadListView has no calls BeginUpdate, EndUpdate, or property ListViewElement...
  11. Hristo
    Admin
    Hristo avatar
    1512 posts

    Posted 28 Feb 2017 Link to this post

    Hi,

    Thank you for writing.

    This forum thread is discussing RadListView for WinForms and the mentioned methods and property are available: http://docs.telerik.com/devtools/winforms/api/html/t_telerik_wincontrols_ui_radlistview.htm.

    Please check the control and product you are using.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  12. m
    m avatar
    2 posts
    Member since:
    Feb 2018

    Posted 20 Feb 2018 Link to this post

    Hi,

    I have a list view which is placed on a tab control. It loads during form initialization. It will list all registered eai items.
    I can select an LV item and unregister it. It clears entry from table and then it re-loads the list view. But it doesn't reflect in ui.
    I have tried listview.Refresh() and .Removeat(), Listview.Update() But nothing worked. Any idea how to refresh list view??
    Here is the code sample:

    public void LoadEAIPlugins()

    {

    try

    lstVwEAIPlugins.Items.Clear();

    for (int i = 1, iteratorTest = g_swServer.m_EAIPluginsInDB.Count; i <= iteratorTest; i++)

    {

    objEAIPlugin = g_swServer.m_EAIPluginsInDB[i];

    AddToGrid2(lstVwEAIPlugins, objEAIPlugin.EAIType, objEAIPlugin.FileNameInDB, objEAIPlugin.InitParams, objEAIPlugin.FileFoundOnDrive);

    lstVwEAIPlugins.Update();

    }

    lstVwEAIPlugins.Update();

    }

     private void AddToGrid2(ListView lstVw, string sText, string sSubItem1, string sSubItem2, bool bStatus)
            {

                ListViewItem lstItem = (ListViewItem)lstVw.Items.Add(sText);
                lstItem.ForeColor = Color.Black; //Normal
                if (!bStatus)
                {
                    ListViewHelper.GetListViewSubItem(lstItem, 3).Text = "FAIL";
                    lstItem.ForeColor = Color.Red; //Red
                }
                else
                {
                    ListViewHelper.GetListViewSubItem(lstItem, 3).Text = "OK";
                }
                ListViewHelper.GetListViewSubItem(lstItem, 1).Text = sSubItem1;
                ListViewHelper.GetListViewSubItem(lstItem, 2).Text = sSubItem2;
            }

  13. Hristo
    Admin
    Hristo avatar
    1512 posts

    Posted 20 Feb 2018 Link to this post

    Hi,

    Thank you for writing.

    You can use the RadListView.ListViewelement.SynchronizeVisualItems method to force the visual elements to update from the data objects. Please note that this should not be done explicitly as long as your data source notifies the list view for changes. This can be accomplished with a Bindinglist which raises the ListChanged events and data objects implementing the INotifyPropertyChanged interface.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  14. m
    m avatar
    2 posts
    Member since:
    Feb 2018

    Posted 20 Feb 2018 in reply to Hristo Link to this post

    Hi there,

    Thank you for responding to my question.

    Regarding the listview update, its actually having the proper count of records in the code debug. but doesnt clear the records in the ui. Is it a list view refresh issue or tab control refresh issue. no idea about it. But when i reload the form, it loads the proper count of records. can you please advise on this

  15. Hristo
    Admin
    Hristo avatar
    1512 posts

    Posted 21 Feb 2018 Link to this post

    Hello,

    Please check if need the rows are removed from your data source. In case the issue persists, please open a support ticket and send us your project so that we can test your actual scenario.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top