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

6 posts, 0 answers
  1. PEIYANG
    PEIYANG avatar
    65 posts
    Member since:
    Jul 2012

    Posted 28 Jul 2012 Link to this post


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



  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 30 Jul 2012 Link to this post

    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.

  3. PEIYANG
    PEIYANG avatar
    65 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    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.
  4. PEIYANG
    PEIYANG avatar
    65 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    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?
  5. PEIYANG
    PEIYANG avatar
    65 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    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! 
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 31 Jul 2012 Link to this post

    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.

Back to Top