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

Index out of range when refreshing grids datasource

11 Answers 2073 Views
GridView
This is a migrated thread and some comments may be shown as answers.
don
Top achievements
Rank 1
don asked on 12 Oct 2015, 02:58 PM

Hi,

 

When i try and refresh the grid's datasource after deleting a row, i get the following error:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

Telerik version: 2015.3.930.40
Framework version: 4.0
Operating system: Windows 10 

 

Here is the refresh code:

        private void RefreshGrid()
        {

            if (legalsManager == null)
            {
                if (_model.DocumentId.HasValue)
                {
                    legalsManager = new DocumentLegalsManager(_model.DocumentId.Value);
                    grdLegals.DataSource = legalsManager.Details;
                    return;
                }

            }
            else
            {
                legalsManager.Refresh();
            }

            if (legalsManager == null)
                return;

            grdLegals.BeginUpdate();
            grdLegals.DataSource = null;
            grdLegals.DataSource = legalsManager.Details;
            grdLegals.EndUpdate();

        }


Stack Trace:

   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at Telerik.WinControls.Data.RadDataView`1.SyncCurrent(TDataItem item)
   at Telerik.WinControls.Data.RadDataView`1.RebuildData(Boolean notify)
   at Telerik.WinControls.Data.RadDataView`1.RefreshOverride()
   at Telerik.WinControls.Data.RadDataView`1.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
   at Telerik.WinControls.Data.RadCollectionView`1.source_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at Telerik.WinControls.Data.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at Telerik.WinControls.Data.RadListSource`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at Telerik.WinControls.Data.RadListSource`1.EndUpdate(Boolean notifyUpdates)
   at Telerik.WinControls.UI.GridViewTemplate.EndUpdate(Boolean notify, DataViewChangedEventArgs e)
   at Telerik.WinControls.UI.MasterGridViewTemplate.EndUpdate(Boolean notify, DataViewChangedEventArgs e)
   at Telerik.WinControls.UI.GridViewTemplate.EndUpdate()
   at Telerik.WinControls.UI.RadGridView.EndUpdate()
   at DeedMagic.Modules.ucDataEntry.RefreshGrid()

It works fine if i add a row, it only happens when i try to delete one. I thought maybe it had to do with the selected rows, so i tried clearing them out but i still get the error. Any suggestions?

 

Thanks


11 Answers, 1 is accepted

Sort by
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 12 Oct 2015, 03:52 PM

I sometimes get weird errors like this when the DataSource is reset during refresh (the grids DataSource property changed) and the grid is set to autogenerate its columns based on the datasource.

My strategy for grids that auto generate their columns based on the DataSource is to data bind only once and on refresh just change the data content but not the definition. For example if the DataSource is a DataTable, I just call Clear() and reload the table. The grid refreshes automatically. No need to reset the grid's DataSource property.

0
don
Top achievements
Rank 1
answered on 12 Oct 2015, 03:53 PM
i'll give this a try and let you know. thanks
0
don
Top achievements
Rank 1
answered on 12 Oct 2015, 07:31 PM
Hi Erwin,

You didn't get me the fix but you got me looking where i needed to get ti straightened out.

My issue was that i had manually built columns in the ui editor and had autogenerate columns set to true.

My fix was setting the autogenerate property to false..

Thanks for the help
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 12 Oct 2015, 08:26 PM

Glad I could be of help.

0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 12 Oct 2015, 08:26 PM

Glad I could be of help.

0
Hristo
Telerik team
answered on 13 Oct 2015, 09:45 AM
Hello guys,

Thank you for writing.

I am glad that you have resolved the problem in your project. The following section in our documentation provides detailed information on the different ways to bind RadGridView Data Binding.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 27 Oct 2015, 07:16 PM

Hi Hristo,

in the past I had problems when using autogenerate columns and changing the datasource property on a visible grid.

The docs not not discourage from doing that - at least I so far didn't find a note. 

For example if your Data Access Layer returns a DataTable, you can not just refresh the grid with

myRadGridView.DataSource = DAL.GetNewDataTable()

even if the new DataTable has the same schema as the old one. The problems are/were different with different versions from Telerik and to my experience also whether you use DeferRefresh() or not. 

Regards

Erwin

 

 

0
Paul
Top achievements
Rank 1
answered on 27 Oct 2015, 09:53 PM

Hi Erwin,

Could try creating a property such as

public BindingSource Binding { get; protected set; }

myRadGridView.DataSource = this.Binding;

Set the BindingSource property to DAL.GetNewDataTable() and call Reset on the BindingSource property to update the grid?

0
Hristo
Telerik team
answered on 28 Oct 2015, 12:16 PM
Hello guys,

Thank you for writing.

@Paul, this is a valid approach and working with a BindingSource property as you suggested is delivering the expected result.

@Erwin, in my sample application I am also able to change the data in the grid by simply assigning a new data table to the RadGridView.DataSource property. I performed my tests with the latest version, if you are still experiencing issues, please open up a support ticket and attach a sample project which I can further investigate.

Please check my code snippet and gif file for reference: 
public partial class Form1 : Form
{
    public BindingSource Binding { get; protected set; }
 
    public Form1()
    {
        InitializeComponent();
 
        this.Binding = new BindingSource();
        this.radGridView1.DataSource = this.Binding;
        this.radGridView1.AutoGenerateColumns = true;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
    }
 
    private void radButton1_Click(object sender, EventArgs e)
    {
        this.Binding.DataSource = this.GetData(int.Parse(this.radTextBox1.Text));
        this.Binding.ResetBindings(true);
    }
 
    private void radButton2_Click(object sender, EventArgs e)
    {
        this.radGridView1.DataSource = this.GetData(int.Parse(this.radTextBox1.Text));
    }
 
    private DataTable GetData(int count)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        for (int i = 0; i < count; i++)
        {
            dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
        }
 
        return dt;
    }

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Paul
Top achievements
Rank 1
answered on 28 Oct 2015, 06:09 PM

If the data schema doesn't change, it would be better to pass false when calling Rest on the Binding. If the schema can change, then true would be required.

this.Binding.ResetBindings(false);

 

0
Hristo
Telerik team
answered on 29 Oct 2015, 09:18 AM
Hi Paul,

Thank you for writing.

Indeed, one should pass the true ​flag only if the schema has been changed. 

Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
don
Top achievements
Rank 1
Answers by
erwin
Top achievements
Rank 1
Veteran
Iron
don
Top achievements
Rank 1
Hristo
Telerik team
Paul
Top achievements
Rank 1
Share this question
or