Refresh a ListView

9 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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 ?
  5. 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
  6. 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
  7. Stefan
    Admin
    Stefan avatar
    2891 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 >>
  8. Jordan
    Jordan avatar
    3 posts
    Member since:
    Mar 2013

    Posted 15 Jun 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?

  9. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 16 Jun 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.
  10. Jordan
    Jordan avatar
    3 posts
    Member since:
    Mar 2013

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

    thanks for getting back to me on this. The SynchronizeVisualItems method did the trick.
Back to Top
UI for WinForms is Visual Studio 2017 Ready