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
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.
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
Glad I could be of help.
Glad I could be of help.
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
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
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?
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
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);
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