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

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

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 ?

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();
Thanks
Richard

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

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?
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


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

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

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