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

When I remove a item in a gridview,why did the gridview in the web page never change? How to rebind them?

5 Answers 80 Views
GridView
This is a migrated thread and some comments may be shown as answers.
PEIYANG
Top achievements
Rank 1
PEIYANG asked on 28 Jul 2012, 11:05 AM

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

Sort by
0
Dimitrina
Telerik team
answered on 30 Jul 2012, 06:35 AM
Hi,

 
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?

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
PEIYANG
Top achievements
Rank 1
answered on 30 Jul 2012, 08:38 AM
My bound ItemsSource defined is list<student>,
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.
0
PEIYANG
Top achievements
Rank 1
answered on 31 Jul 2012, 01:23 AM
This is the codes:
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?
0
PEIYANG
Top achievements
Rank 1
answered on 31 Jul 2012, 01:35 AM
I debug the codes above, and see RGVResult.ItemsSource  count=4,RGVResult.Items.count=0; How to change my codes to Consistent with my desire? Thank you! 
0
Dimitrina
Telerik team
answered on 31 Jul 2012, 03:42 PM
Hi,

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.

Tags
GridView
Asked by
PEIYANG
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
PEIYANG
Top achievements
Rank 1
Share this question
or