How to get Items selected

Hi, how to get values from SFCombobox and send it to HttpClient. I need save values into Mysql Database.

My XAML:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:combobox="clr-namespace:Syncfusion.XForms.ComboBox;assembly=Syncfusion.SfComboBox.XForms"
xmlns:local="clr-namespace:MVP.ViewModels" xmlns:inputLayout="clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms"
x:Class="MVP.Pages.ConsoleMessages"
Title="Console">
<ContentPage.BindingContext>
<local:ViewModelGroup/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<StackLayout>
<Label Text="Users">
<combobox:SfComboBox
x:Name="valoresObtenidos"
MultiSelectMode="Token"
DisplayMemberPath="Quien_Nombre"
SelectedValue="{Binding SelectedValue, Mode=TwoWay}"
SelectedValuePath="Quien_ID"
IsEditableMode="True"/>
</StackLayout>
<StackLayout>
<Button
x:Name="ConsolaMsg_BtnSend"
Text="Send"
Clicked="BotonEnviarMensaje"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>

Code Behind:

using System;
using System.Collections.Generic;
using System.Net.Http;
using MVP.Modelos;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace MVP.Pages
{
public partial class ConsoleMessages : ContentPage
{
private List<ModeloQuienes> modeloQuienesDatos;

public ConsoleMessages()
{
InitializeComponent();
}

private void ComboBox_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)
{
string jsonResultados = string.Empty;
try
{
HttpClient Cliente = new HttpClient();
Cliente.BaseAddress = new Uri("http://www.mvppro.us");
string url = string.Format("/xamarin/json_loadusers.php");
var resultado = await Cliente.GetAsync(url);
jsonResultados = resultado.Content.ReadAsStringAsync().Result;
}
catch (Exception)
{
await DisplayAlert("Error", "Your Smartphone Should Be Connected to Internet", "Ok");
return;
}
modeloQuienesDatos = JsonConvert.DeserializeObject<List<ModeloQuienes>>(jsonResultados);
valoresObtenidos.DataSource = modeloQuienesDatos;
UserDialogs.Instance.HideLoading();
}

// SEND SFCOMBOBOX
private async void BotonEnviarMensaje(System.Object sender, System.EventArgs e)
{
// CODE HERE
}
}
}

Thanks and appreciate your help !!


15 Replies

SS Suganya Sethuraman Syncfusion Team October 18, 2021 12:25 PM UTC

Hi EDGAR,

Greetings from Syncfusion.

We have analyzed your query. We recommend that you use the SelectedItem API of SfComboBox to retrieve the selected item of SfComboBox.

Code snippet
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
                                           var selectedItems = cmbFavList.SelectedItem; 
        } 
 

Please have a sample for your reference,

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxSample885644423

Please check if the sample satisfies your requirement and let us know if you have any concerns.

Regards,
Suganya Sethuraman.
 



EG EDGAR GARCIA October 19, 2021 01:27 AM UTC

thanks, but your sample not working. check it.


when. i add this line:


private async void Button_Clicked(object sender, EventArgs e)

{

var selectedItems = cmbFavList.SelectedItem;

await DisplayAlert("", selectedItems.ToString(),"");

}

Crash the app



FE Fernando replied to EDGAR GARCIA October 19, 2021 01:46 AM UTC

Try it: await DisplayAlert("Alert", selectedItems.ToString(),"OK");


Display alert cant be empty

Then change to

if ( cmbFavList .SelectedItem != null)

            {

                string value = ( cmbFavList  .SelectedItem as YourModel). Quien_ID ;

             }




EG EDGAR GARCIA replied to Fernando October 19, 2021 02:31 AM UTC

thanks Fernando, but not working.


the code:


if(valoresObtenidos.SelectedItem != null)

{

    var value = (valoresObtenidos.SelectedItem as ModeloQuienes).Quien_ID;

    await DisplayAlert("test", Value, "Ok");

}

 Crash 



EG EDGAR GARCIA replied to Fernando October 19, 2021 02:38 AM UTC

using the same example:


private async void Button_Clicked(object sender, EventArgs e)

{

    if (cmbFavList.SelectedItem != null)

    {

        string value = (cmbFavList.SelectedItem as Employee).Name;

        await DisplayAlert("Alert", value, "OK");

    }

}


i received this message: 

System.NullReferenceException has been thrown






SS Suganya Sethuraman Syncfusion Team October 19, 2021 06:41 AM UTC

Hi EDGAR,

Sorry for the inconvenience.

We would like to let you know that in SfComboBox's Token mode, we can get the SelectedItem as an ObservableCollection<object>. So, using the code snippet below, you can get the SelectedItem.

Code snippet
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
            ObservableCollection<object> tempCollection = new ObservableCollection<object>(); 
            if (cmbFavList.SelectedItem is ObservableCollection<object>) 
            { 
                tempCollection = cmbFavList.SelectedItem as ObservableCollection<object>; 
            } 
        } 

Please check and let us know if you have any concerns.

Regards,
Suganya Sethuraman.
 



EG EDGAR GARCIA replied to Suganya Sethuraman October 20, 2021 02:49 AM UTC

ok, but how can i Access Value From ObservableCollection in this example?

        private void Button_Clicked(object sender, EventArgs e)

        {

            ObservableCollection<object> tempCollection = new ObservableCollection<object>();

            if (cmbFavList.SelectedItem is ObservableCollection<object>)

            {

                tempCollection = cmbFavList.SelectedItem as ObservableCollection<object>;

                DisplayAlert("Selected Items", tempCollection.ToString(), "Ok");

            }

        }

DisplayAlert show this when Button_Clicked is pressed

system.collections.objectmodel.ObservableCollection


i would like show the names or values selected.

Thanks.



EG EDGAR GARCIA replied to EDGAR GARCIA October 20, 2021 11:58 PM UTC

???? Any solution ?



SS Suganya Sethuraman Syncfusion Team October 21, 2021 07:30 AM UTC

Hi EDGAR,

We would like to let you know that in SfComboBox's Token mode, we can get the SelectedValue as List<string>. So, using the code snippet below, you can get and display the selected value.

Code snippet
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
                                           List<string> tempCollection = new List<string>(); 
            if (cmbFavList.SelectedValue is List<string>) 
            { 
                tempCollection = cmbFavList.SelectedValue as List<string>; 
            } 
 
            var result = string.Join(", ", tempCollection); 
                                           DisplayAlert("Selected Items", result, "Ok"); 
                             } 


Please check and let us know if you have any concerns.

Regards,
Suganya Sethuraman.
 



EG EDGAR GARCIA November 6, 2021 05:11 PM UTC

Am dissapointed, I have tried everything. Can you check the code attached ?

When i clicked so return empty list. Please check it 


Attachment: Archive_b4037b96.zip


RS Ruba Shanmugam Syncfusion Team November 8, 2021 12:54 PM UTC

Hi EDGAR,

Greetings from Syncfusion.

We have validated your query and we will update the complete details on or before the Nov 10, 2021. We appreciate your patience until then.

Regards,
Ruba Shanmugam



EG EDGAR GARCIA November 10, 2021 02:31 AM UTC

Ok. Am waiting your response. appreciate your help.

Kind Regards,

Edgar



SS Suganya Sethuraman Syncfusion Team November 10, 2021 11:43 AM UTC

Hi EDGAR,

Sorry for the inconvenience. 

We have modified the sample based on your requirement. Please have a sample for your reference,

Code snippet:
 
                             private void Button_Clicked(object sender, EventArgs e) 
                             { 
                                           List<string> tempCollection = new List<string>(); 
                                            
                                           if (employeeViewModel.FooObject is ObservableCollection<object>) 
                                           { 
                                                          for (int i = 0; i < (employeeViewModel.FooObject as ObservableCollection<object>).Count; i++) 
                                                          { 
                                                                        tempCollection.Add(((employeeViewModel.FooObject as ObservableCollection<object>)[i] as Employee).Name); 
                                                          } 
                                           } 
 
                                           var result = string.Join(", ", tempCollection); 
                                           DisplayAlert("Selected Items", result, "Ok"); 
                             } 

Sample:  https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfComboBox2020161405

Please check and let us know if you have any concerns.

Regards,
Suganya Sethuraman.
 



EG EDGAR GARCIA November 11, 2021 03:38 AM UTC

Great !!!!! Thanks, that works for me !!




SS Suganya Sethuraman Syncfusion Team November 11, 2021 05:52 AM UTC

Hi EDGAR,

Thanks for the update.

We are glad that your issue is resolved. Please let us know if you require any further assistance.

Regards,
Suganya Sethuraman.
 
 


Loader.
Up arrow icon