I'm binding a WebDataGrid on a OnSelectedIndexChanged event which works fine. However I'm using an UnboundCheckBoxField and if I select checkbox in the header, then deselect one or more rows, and change the selection from the dropdownlist, I get the above error.
I am not concerned with keeping previously checked values so I just clear DataSource, dehydrate, and rebind.
protected void serviceTypeFamilyDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{ using (DropDownList serviceTypeFamilyDropDown = sender as DropDownList) { servicesGrid.ClearDataSource(); servicesGrid.DataSource = ExportableServiceVisits.GetServiceVisitsByServiceTypeFamily( serviceTypeFamilyDropDown.SelectedItem.Value.GetDoubleOrIntValue<int>(), serviceDatePicker.Date); servicesGrid.DataBind(); }}
Following suggestions from this forum I created a property on the page and upon OnSelectedIndexChanged I updated the data of the property then on servicesGrid_OnPreRender, I cleared the DataSource, set the DataSource to the property and rebound. However, the error was thrown before OnPreRender() was executed.
private IEnumerable<ExportableServiceVisits> ServiceVisits { get; set; }
protected void serviceTypeFamilyDropDown_OnSelectedIndexChanged(object sender, EventArgs e){ using (DropDownList serviceTypeFamilyDropDown = sender as DropDownList) { ServiceVisits = ExportableServiceVisits.GetServiceVisitsByServiceTypeFamily( serviceTypeFamilyDropDown.SelectedItem.Value.GetDoubleOrIntValue<int>(), serviceDatePicker.Date); }}
protected void servicesGrid_OnPreRender(object sender, EventArgs e){ servicesGrid.ClearDataSource(); servicesGrid.DataSource = ServiceVisits; servicesGrid.DataBind();}
What's the problem?
Hello Kyle,
I couldn't reproduce the issue. Please send me an isolated working sample to see what exactly is the problem.
I'm looking forward your reply.
Attached is an example.
Steps to recreate:
Hi Aleksandar,
Can you please explain, what do you mean by saing that's not recommended approach?
Helo Kyle,
As I say you can set to your grid EnableDataViewState="true". This will pass without errors.
If you have further questions, please feel free to contact us.
But I don't care about the saved values onSelectedIndexChange. I cleared the grid and bound to the new data. How do I do this without getting an error?
Hello Wingate,
I reproduced your sample and I found why this error is thrown. The problem is that you update your value in your grid and in onSelectionIndexChange event you didn't rebind your dataSource and the edited values isn't saved in grid. On every postBack after every update you should rebind your dataSource with edited values in grid. You can set to your gird EnableDataViewState="true". I want to clarify that this is not recommended approach.