obtaining second field of selected item

Hello,

I have what I hope is a simple question. I have an observablecollection comprised as follows:

public ObservableCollection<JobTask> JobTasks { get; set; }

public TimesheetViewModel()

{

    Jobs = new ObservableCollection<Job>

    {

        new Job() { JobName = "499 - Job One", JobNo = "499" },

        new Job() { JobName = "500 - Job Two", JobNo = "500" },

        new Job() { JobName = "501 - Job Three", JobNo = "501" }

    };

The JobName is bound to the sfCombobox in the xaml page:

<editors:SfComboBox

    x:Name="cboJobs"

    ItemsSource="{Binding Jobs}"

    DisplayMemberPath="JobName"

    SelectionChanged="OncboJobChanged"

    SelectedValuePath="JobName"

/>

I use the SelectedValue to obtain the JobName:

private void OncboJobChanged(object sender, Syncfusion.Maui.Inputs.SelectionChangedEventArgs e)

{

    selectedValue.Text = cboJobs.SelectedValue.ToString();

}

but how do I also obtain the JobNo so that I can write this to SQLite at the same time?

private async void saveButton_Clicked(object sender, EventArgs e)

{

        if( _editTimesheetID == 0 )

    {

        //add job

        await _databaseContext.Create(new Timesheet

        {

            ID = _editTimesheetID,

            JobName = selectedValue.Text,

           JobNo = ??

        });


Thanks for your time.

Regards

Ross


4 Replies

AJ AhamedAliNishad JahirHussain Syncfusion Team January 2, 2024 01:50 PM UTC

Hi Ross,


After reviewing your query, we have created a sample based on the provided information. We want to inform you that you can retrieve the JobName and JobNo in the SelectionChanged event using the SelectedItem property in SfComboBox, as shown in the code snippet below. Please review the attached sample and let us know the details.


Cod Snippet:


Mainpage.xaml

 

        <StackLayout VerticalOptions="Center" HorizontalOptions="Center">

 

            <combobox:SfComboBox x:Name="combo"

           DisplayMemberPath="JobName"

           SelectionChanged="SfComboBox_SelectionChanged"

           ItemsSource="{Binding Jobs}"

                    SelectedValuePath="JobName"

                      >

 

 

            </combobox:SfComboBox>

          

        </StackLayout>

 

Mainpage.xaml.cs

 

        private void SfComboBox_SelectionChanged(object sender, Syncfusion.Maui.Inputs.SelectionChangedEventArgs e)

        {

            var gettingJobName = (combo.SelectedItem as Job).JobName.ToString();

            var gettingJobNo = (combo.SelectedItem as Job).JobNo.ToString();

        }

 


Regards,

Ahamed Ali Nishad.


Attachment: SfCombo_(6)_df15a9fb.zip


RB Ross Bell Syer January 5, 2024 09:30 PM UTC

Thanks very much for your quick response. Your answer above worked great.

I've managed to obtain both the job name and job no from the combobox on selection.

I have one small problem now though and that is that when trying to repopulate these values from SQLite using for example:

cboJobs.SelectedItem = timesheet.JobName;

the combobox instance has disappeared:

System.NullReferenceException: 'Object reference not set to an instance of an object.'




RB Ross Bell Syer January 5, 2024 11:32 PM UTC

...actually I managed to sort out the above problem by keeping a record of the combobox index selected for each combobox in SQLite. Then when populating the combobox for editing an existing record I used this index number.

Not sure its the most efficient way but it seemed to work.



BV Brundha Velusamy Syncfusion Team January 8, 2024 07:53 AM UTC

Hi Ross,


To retrieve the selected item values in the SfComboBox control, you can utilize the SelectedIndex and SelectedItem properties. These properties provide programmatic access to the currently selected item in the SfComboBox.


Please refer the  below help documentation link for more information about SfComboBox

https://help.syncfusion.com/maui/combobox/selection#programmatic-selection


If you have any further questions or need additional assistance, please feel free to reach out.


Regards,

Brundha V


Loader.
Up arrow icon