locked
IndexOutOfRange in DataGrid RRS feed

Answers

  • I expect this is a bug.  Have you tried setting SelectedItem = null or SelectedIndex = -1 before you clear the ItemsSource?

    Friday, August 29, 2008 5:42 PM

All replies

  • I expect this is a bug.  Have you tried setting SelectedItem = null or SelectedIndex = -1 before you clear the ItemsSource?

    Friday, August 29, 2008 5:42 PM
  • I tried the SelectedIndex = -1; thing. It doesn't work.

    SelectedItem = null;  - I don't know.

    Friday, August 29, 2008 6:28 PM
  • Hi Bitdisaster,

    I have downloaded your demo.  However,  I don't see you to modify its datasource.  Indeed, you just bind a new datasource to the DataGrid's ItemsSource simply.   

    public partial class Page : UserControl
        {
    
            ObservableCollection _mydata;
            public Page()
            {
                InitializeComponent();
                _mydata = new ObservableCollection { new Foo { Bar = "Hallo" }, new Foo {Bar = "Welt" } };
                MyDataGrid.ItemsSource = _mydata;
            }
    
            private void OnClear(object sender, RoutedEventArgs e)
            {
                _mydata.Clear();
            }
    
            private void OnChangeSource(object sender, RoutedEventArgs e)
            {
                MyDataGrid.ItemsSource = new ObservableCollection();
            }
        }
    
        public class Foo
        {
            public string Bar { get; set; }
        }

    However, below are the video tutorials.

    Getting Started with the DataGrid

    Insert, Update, Delete with the DataGrid

    Please follow the steps strictly.

    Best regards,

    Jonathan 

     

     

    Tuesday, September 2, 2008 5:03 AM
  • It's just a demo, and of course I dont't modify the content of the ItemSource!

    But in my real application I have to change the ItemSource at runtime like in the demo and I get an exception under the circumstances decribed above. So I will look into the videos, but I guess it doesn't change the fact that this is a bug and not a feature.  :-Confused

     

    Cheers

    Jan

    Tuesday, September 2, 2008 2:06 PM
  • Hi Bitdisaster,

    Please first set DataGrid's ItemsSource to null, then to the modified datasource and have a test.

     Best regards,

    Jonathan

    Tuesday, September 2, 2008 10:01 PM
  • Nope, it doesn't work. Same exception when I set it to null.

    Wednesday, September 3, 2008 12:19 AM
  • Ok, I had another test on Shawn's suggestion.

    SelectedItem = null; does the trick while SelectedIndex = -1; don't.

     

    So I marked Shawns post as answer.

     

     

    Wednesday, September 3, 2008 12:21 AM
  • Hello,

    I was searching this code segment for few hrs. I was modifying the associated itemsource and was trying to "re-bind"...

    I got the result..

    Thanks a lot..Keep posting valuable info.

    Best Regards from me

    Monday, January 26, 2009 2:46 AM