When I remove a item in a gridview,why did the gridview in the web page never change? How to rebind them?
Hear is my codes:
for(int i=radgrivew1.items.count-1;i>=0;i--)
{
if( condiction) //condiction sometimes be true
radgrivew1.items.remove(item[i]);
}
5 Answers, 1 is accepted
I have tested the described case when the GridView is bound to an ObservableCollection of objects and I did not encounter any problems.
May I ask you to share how is your bound ItemsSource defined? Is a CollectionChanged event raised for the bound collection on removing the item?
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Is a CollectionChanged event raised for the bound collection on removing the item?
NO,Do I need a event,what is the event?I think I remove the item,It' no doubt that the gridview will change.
TestData:
public TestData(double xvaule, double yvalue)
{
this.Xvalue = xvaule;
this.Yvalue = yvalue;
}
public double Xvalue
{
get;
set;
}
public double Yvalue
{
get;
set;
}
MainPage:
List<TestData> a = new List<TestData>();
a.Add(new TestData(1, 1));
a.Add(new TestData(2, 2));
a.Add(new TestData(3, 3));
a.Add(new TestData(4, 4));
RGVResult.ItemsSource = a;
for (int i = RGVResult.Items.Count - 1; i >= 0; i--)
{
TestData td=RGVResult.Items[i] as TestData;
if (td.Xvalue==2 ) //condiction sometimes be true
RGVResult.Items.RemoveAt(i);
}
I do not want to operate a and remove element from a and then rebind a to gridview,because sometimes I can't get the datasource directly,Since I remove the item from the gridview in UI,why does the gridview never change?
If you try to access the Items collection directly after setting the ItemsSource for the GridView, then it will have 0 items as the data has still not been loaded.
You could change your code like so:
for
(
int
i = a.Count - 1; i >= 0; i--)
{
TestData td=a[i]
as
TestData;
if
(td.Xvalue==2 )
//condiction sometimes be true
a.RemoveAt(i);
}I hope this helps.
Greetings,
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.