I have two picker. second picker is dependent on first picker. Both pickers are bind from Service. I am using dictionary object to bind data to picker. I am not using MVVM pattern.
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Global Declaration :
Dictionary<string, string> DicObjActivityType;
Dictionary<string, string> DicObjSelfActivity;
First Picker selectedIndexChange event
private async void PckrActivityType_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender as Picker == null)
return;
else
{
var objActivityType = sender as Picker;
var Key = DicObjActivityType.FirstOrDefault(x => x.Value == objActivityType.Items[objActivityType.SelectedIndex]).Key;
PickedActivityType = Key;
if (Key != "0")
{
PckrSelfActivity.IsEnabled = true;
await CallGetWebService_SelfActivity(Key);
if (PckrSelfActivity.IsEnabled == true)
{
PckrSelfActivity.Items.Clear();
foreach (string items in DicObjSelfActivity.Values)
{
PckrSelfActivity.Items.Add(items);
}
}
}
else
{
PckrSelfActivity.IsEnabled = false;
}
}
}
Call Service of second picker
private async Task CallGetWebService_SelfActivity(string strkey)
{
try
{
var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_LstListComboData>(ServiceURL.GetSelfActivity + "ActivityTypeCd=" + strkey);
if (response.Flag == true)
{
DicObjSelfActivity = new Dictionary<string, string>();
DicObjSelfActivity.Add("0", "--Select--");
if (response.lstListComboData != null)
{
foreach (ServiceClasses.LstListComboData Items in response.lstListComboData)
{
DicObjSelfActivity.Add(Items.Value, Items.Text);
}
}
}
else
{
PckrSelfActivity.IsEnabled = false;
}
}
catch (Exception e)
{
await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
}
}
I refer following link to solve this issue
https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception
but didn't find solution.
We can't clear picker if any of the value is selected?
I don't want to use BindablePicker cutom control.
I'm not sure if by "clear the picker" you mean reset it such that it doesn't show any items as selected. But if that's what you want, then you can do the following:
Set SelectedIndex = -1
. This resets the picker such that no items are selected.
Once you set SelectedIndex = -1
, the selectedIndexChange
event handler is called, and that's causing the
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Indeed yes, the SelectedIndex
is -1
, so it's complaining that the index must be non-negative.
if
statement that encloses your SelectedIndexChange
event handler so that the handler only executes if (SelectedIndexChange != -1)
.In summary, do this:
YourPicker.SelectedIndex = -1;
YourPicker.SelectedIndexChanged += (sender, e) =>
{
if (YourPicker.SelectedIndex != -1)
{
//Do your stuff
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With