Results 1 to 5 of 5

Thread: Can't resolve System.ArgumentOutOfRangeException

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,972

    Can't resolve System.ArgumentOutOfRangeException

    I'm trying to bind a datatable to a WPF DataGrid control. The control resides within a Tab Control. Below is my code:

    Code:
            private void LoadEmployersToGrid()
            {
                if (DatabaseConnection())
                {
                    DataTable dt = new DataTable();
    
                    Employer emp = new Employer();
    
                    dt = emp.GetAllEmployerRecords(cnn);
    
                    Grid1.AutoGenerateColumns = true;
                    Grid1.ItemsSource = dt.DefaultView;
    
                    Grid1.Columns[0].Visibility = Visibility.Hidden;   //Exception is thrown on this line 'Index was out of range. Must be non-negative and less than the size of the collection.
                    Grid1.Columns[2].Visibility = Visibility.Hidden;
                    Grid1.Columns[3].Visibility = Visibility.Hidden;
                    Grid1.Columns[4].Visibility = Visibility.Hidden;
                }
            }
    When I run in debug mode, the "dt" contains the right amount of records and columns. However, after I do the assignment of the dt.DefaultView to the ItemSource...the Grid1 columns count is zero. What am I doing wrong?

    Thanks,
    Blake

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,193

    Re: Can't resolve System.ArgumentOutOfRangeException

    My guess would be that the columns don't actually exist until the current method has completed. The grid itself has an AutoGeneratingColumn event and that may well not have been raised and handled while you're still executing that method on the UI thread. If you handle that event then you can manipulate the columns as they are created.

    I have to say though, you're probably wasting a lot of time learning how to use WPF in the same way that you used WinForms. If you want to do it that way then that's your prerogative but no one really uses WPF that way in the wild. At the very least, people will configure WPF data-binding in XAML and pretty much anyone who does anything with WPF uses the MVVM design pattern. Maybe you feel that it's too hard to jump straight into that or you maybe you don't realise what the different is. If you haven't already, I'd suggest that you look into it to see what "proper" use of WPF looks like. You can then decide whether learning how to use it in a way that you basically never will is a good use of your time.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,972

    Re: Can't resolve System.ArgumentOutOfRangeException

    I really don't know which road to take with WPF yet.

    Thanks for the advice jmc.
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,193

    Re: Can't resolve System.ArgumentOutOfRangeException

    If you're definitely using WPF then MVM is the only road to take. You can use it like WinForms to learn some of the basics but you're not using it as intended if you're not data-binding in XAML and you're not using it as any serious developer does if you're not using MVVM.

  5. #5
    New Member
    Join Date
    Jun 2020
    Posts
    1

    Re: Can't resolve System.ArgumentOutOfRangeException

    The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a list if that offset doesn't exist.

    Typically, an ArgumentOutOfRangeException results from developer error. Instead of handling the exception in a try/catch block, you should eliminate the cause of the exception or, if the argument is returned by a method call or input by the user before being passed to the method that throws the exception, you should validate arguments before passing them to the method.
    Last edited by si_the_geek; Jun 29th, 2020 at 08:02 AM. Reason: removed irrelevant link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width