This is a migrated thread and some comments may be shown as answers.

Refresh a ListView

14 Answers 2800 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 25 Apr 2012, 05:38 PM
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

14 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 26 Apr 2012, 04:51 PM
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
0
Robert
Top achievements
Rank 1
answered on 26 Apr 2012, 05:51 PM
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 ?
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 27 Apr 2012, 09:04 AM
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
0
Robert
Top achievements
Rank 1
answered on 28 Apr 2012, 08:29 AM
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
0
Stefan
Telerik team
answered on 30 Apr 2012, 11:15 AM
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 >>
0
Thomas
Top achievements
Rank 1
answered on 15 Jun 2016, 09:47 PM

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?

0
Hristo
Telerik team
answered on 16 Jun 2016, 04:14 PM
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.
0
Thomas
Top achievements
Rank 1
answered on 16 Jun 2016, 04:42 PM
thanks for getting back to me on this. The SynchronizeVisualItems method did the trick.
0
Gerry
Top achievements
Rank 1
answered on 27 Feb 2017, 06:16 PM
It would be nice to see this updated. RadListView has no calls BeginUpdate, EndUpdate, or property ListViewElement...
0
Hristo
Telerik team
answered on 28 Feb 2017, 10:59 AM
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.
0
m
Top achievements
Rank 1
answered on 20 Feb 2018, 02:11 PM

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

0
Hristo
Telerik team
answered on 20 Feb 2018, 02:49 PM
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.
0
m
Top achievements
Rank 1
answered on 20 Feb 2018, 06:23 PM

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

0
Hristo
Telerik team
answered on 21 Feb 2018, 02:34 PM
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.
Tags
ListView
Asked by
Robert
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Robert
Top achievements
Rank 1
Stefan
Telerik team
Thomas
Top achievements
Rank 1
Hristo
Telerik team
Gerry
Top achievements
Rank 1
m
Top achievements
Rank 1
Share this question
or