locked
How do I sort the listview on selecteditem in the combobox RRS feed

  • Question

  • I have a listview consist of people name and gender, then I want to sort them according to gender from the combobox when male selected listview only display the male name. Likewise for female.I am using a WCF Service to communicate with the sql server database.


    Thanks, Smiths



    Friday, July 5, 2013 2:10 AM

Answers

  • Hi Smith,
    First I want to tell you why are you calling the Web Service twice ?? :)

    ObservableCollection<People> PeopleList;
    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }
    
    private async void cbDiploma_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if ((cbDiploma.SelectionBoxItem as People).gender != null)
                {
                    
    
                    this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender== (cbDiploma.SelectionBoxItem as People).gender);
                }
                else
                {
                    return;
                }
                
            }
    
    private async void btnSort_Click(object sender, RoutedEventArgs e)
            {
                this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender==(cbDiploma.SelectionBoxItem as People).gender);
    
                //string seletedGender = (cbDiploma.SelectionBoxItem as People).gender.ToString();
            }
    
    
    private async void btnClear_Click(object sender, RoutedEventArgs e)
            {
                
    
                this.vmListView.ItemsSource = PeopleList;
            }

    Now you're just calling the web service one at OnNavigatedTo event then make all your operation on this list which is much faster.

    second why the combobox selectionchange isn't fire !! can you please share the xaml code with me ??

    Thanks


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    • Marked as answer by Reid Smiths Sunday, July 7, 2013 7:46 AM
    Saturday, July 6, 2013 8:54 AM
  • Hi Smiths,

    you should use SelectedItem not selectionBoxItem

    private async void cbDiploma_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if ((cbDiploma.SelectedItem as People).gender != null)
                {
                    
    
                    this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender== (cbDiploma.SelectedItem as People).gender);
                }
                else
                {
                    return;
                }
                
            }

    Try it and it will work :)
    Regards,


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)


    Saturday, July 6, 2013 4:17 PM
  • Hi,
    use this

    private async void btnClear_Click(object sender, RoutedEventArgs e)
            {
                this.vmListView.ItemsSource = PeopleList;
            }


    if you are using

    ObservableCollection<People> PeopleList;
    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }

    or you can call the web service again which will be slow.
    Please mark all the answers that helped you as an answer :)
    Regards,


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    • Marked as answer by Reid Smiths Sunday, July 7, 2013 7:46 AM
    Sunday, July 7, 2013 7:03 AM

All replies

  • I want to combine the combobox with sort button such that selection changed the listview will update itself.

    Thanks, Smiths

    Friday, July 5, 2013 8:31 AM
  • Hi

    see this code

    l.ItemsSource = myList.Where(i => i.gender == "male or female ").OrderBy(i=>i.age);

    myList is the list or collection of the class you are using
    now when you use where you can check to choose what you want ! like gender ==1 , gender == male or whatever you are using

    OrderBy sort the collection or list as you want  by age , name , ....

    now check what you want and in the item selection change event check what is selected and sort or select the items you want

    Thanks, 


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Friday, July 5, 2013 12:03 PM
  • But I am using SQL Server database not the LINQ data.

    This is from my service.

    public ObservableCollection<People> GetByGender(People sortByGender)
            {
                ObservableCollection<People> InfoDetail = new ObservableCollection<People>();
                {
                    //selectedgender.gender = "Male";
    
                    conn1.Open();
                    SqlCommand cmd = new SqlCommand("SELECT * FROM ExampleTable WHERE Gender = '"+sortByGender.gender+"'", conn1);
                    //SqlCommand cmd = new SqlCommand("SELECT * FROM ExampleTable", conn1);
    
    
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            People info = new People();
                            info.name = dt.Rows[i]["Name"].ToString();
                            info.gender = dt.Rows[i]["Gender"].ToString();
                            InfoDetail.Add(info);
                        }
                    }
                    conn1.Close();
                }
                return InfoDetail;
            }
    
            public ObservableCollection<People> GetAll()
            {
                ObservableCollection<People> InfoDetail = new ObservableCollection<People>();
                {
                    //selectedgender.gender = "Male";
    
                    conn1.Open();
                    //SqlCommand cmd = new SqlCommand("SELECT * FROM ExampleTable WHERE Gender = '" + sortByGender.gender + "'", conn1);
                    SqlCommand cmd = new SqlCommand("SELECT * FROM ExampleTable", conn1);
    
    
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            People info = new People();
                            info.name = dt.Rows[i]["Name"].ToString();
                            info.gender = dt.Rows[i]["Gender"].ToString();
                            InfoDetail.Add(info);
                        }
                    }
                    conn1.Close();
                }
                return InfoDetail;
            }

    Than I call it in the code behind. But cannot really get the combobox selectionchanged method works.

    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                ObservableCollection<People> PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }
    
    
    private async void cbDiploma_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if ((cbDiploma.SelectionBoxItem as People).gender != null)
                {
                    ObservableCollection<People> PeopleList = await objService.GetByGenderAsync(new ServiceReference1.People() { gender = (cbDiploma.SelectionBoxItem as People).gender });
    
                    this.vmListView.ItemsSource = PeopleList;
                }
                else
                {
                    return;
                }
                
            }
    
    private async void btnSort_Click(object sender, RoutedEventArgs e)
            {
                ObservableCollection<People> PeopleList = await objService.GetByGenderAsync(new ServiceReference1.People() { gender = (cbDiploma.SelectionBoxItem as People).gender });
    
                this.vmListView.ItemsSource = PeopleList;
    
                //string seletedGender = (cbDiploma.SelectionBoxItem as People).gender.ToString();
            }
    
            private async void btnClear_Click(object sender, RoutedEventArgs e)
            {
                ObservableCollection<People> PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }


    Thanks, Smiths

    Friday, July 5, 2013 3:04 PM
  • Hi Smith,
    First I want to tell you why are you calling the Web Service twice ?? :)

    ObservableCollection<People> PeopleList;
    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }
    
    private async void cbDiploma_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if ((cbDiploma.SelectionBoxItem as People).gender != null)
                {
                    
    
                    this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender== (cbDiploma.SelectionBoxItem as People).gender);
                }
                else
                {
                    return;
                }
                
            }
    
    private async void btnSort_Click(object sender, RoutedEventArgs e)
            {
                this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender==(cbDiploma.SelectionBoxItem as People).gender);
    
                //string seletedGender = (cbDiploma.SelectionBoxItem as People).gender.ToString();
            }
    
    
    private async void btnClear_Click(object sender, RoutedEventArgs e)
            {
                
    
                this.vmListView.ItemsSource = PeopleList;
            }

    Now you're just calling the web service one at OnNavigatedTo event then make all your operation on this list which is much faster.

    second why the combobox selectionchange isn't fire !! can you please share the xaml code with me ??

    Thanks


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    • Marked as answer by Reid Smiths Sunday, July 7, 2013 7:46 AM
    Saturday, July 6, 2013 8:54 AM
  • This is the XAML code. Thanks

    <ComboBox x:Name="cbGender" HorizontalAlignment="Left" Margin="773,330,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="cbGender_SelectionChanged">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="2">
                            <TextBlock Text="{Binding gender}"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
    </ComboBox>


    Thanks, Smiths

    Saturday, July 6, 2013 11:11 AM
  • The code is working for me :) are you sure that the selectionChange event isn't firing ? check it by putting a break point the beginning of the event handler  

    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Saturday, July 6, 2013 11:52 AM
  • Which means when you choose the male/female in the combo box it automatically sort the listview by combobox selecteditem? Cause I keep getting the error "Object reference not set to an instance of an object."

    Thanks, Smiths


    • Edited by Reid Smiths Saturday, July 6, 2013 12:34 PM adding error message
    Saturday, July 6, 2013 12:31 PM
  • Hi Smiths,

    you should use SelectedItem not selectionBoxItem

    private async void cbDiploma_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if ((cbDiploma.SelectedItem as People).gender != null)
                {
                    
    
                    this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender== (cbDiploma.SelectedItem as People).gender);
                }
                else
                {
                    return;
                }
                
            }

    Try it and it will work :)
    Regards,


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)


    Saturday, July 6, 2013 4:17 PM
  • Thanks, It works but if I want to display all again ?

    Thanks, Smiths

    Sunday, July 7, 2013 6:26 AM
  • Hi,
    use this

    private async void btnClear_Click(object sender, RoutedEventArgs e)
            {
                this.vmListView.ItemsSource = PeopleList;
            }


    if you are using

    ObservableCollection<People> PeopleList;
    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                PeopleList = await objService.GetAllAsync();
    
                this.vmListView.ItemsSource = PeopleList;
            }

    or you can call the web service again which will be slow.
    Please mark all the answers that helped you as an answer :)
    Regards,


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    • Marked as answer by Reid Smiths Sunday, July 7, 2013 7:46 AM
    Sunday, July 7, 2013 7:03 AM
  • If i use the combobox can ?


    Thanks, Smiths

    Sunday, July 7, 2013 7:13 AM
  • Yes , just write
    cbGender.ItemsSource=PeopleList;

    Regards,

    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Sunday, July 7, 2013 7:22 AM
  • I mean like to see the whole list

    Thanks, Smiths

    Sunday, July 7, 2013 7:45 AM
  • Hi Smiths,
    Sorry but I really can't get what you want :) 

    Thanks,

    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Sunday, July 7, 2013 9:46 AM
  • Which means in the combobox I can choose between the male and female and also back to original.

    Thanks, Smiths

    Sunday, July 7, 2013 12:23 PM
  • Can you also explain this part "
     this.vmListView.ItemsSource = PeopleList.Where(i=>i.gender== (cbDiploma.SelectedItem as People).gender);
    " to me thanks.

    Thanks, Smiths

    Sunday, July 7, 2013 1:27 PM
  • Sure.

    Let's break it into small pieces

    cbDiploma.SelectedItem : is the People item that we selected from the cbDiploma comboBox but we should cast it as people so we can uses it's attributes (name , gender , ..)

    PeopleList.Where : Is a lambada expression to write a LINQ expression  which means select * from PeopleList Where gender= ourInput.

    that's all :)


     

    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Sunday, July 7, 2013 6:33 PM