Need to remove already selected value in wpf datagrid combo box

K, Arivalagan 1 Reputation point
2022-08-26T06:45:19.88+00:00

Hi,

From the MVVM WPF application, We have WPF datagrid and one of the column is combo box. If user selected value in the combobox on first row should not be visible on the combobox in the next row. Please help.

Thanks

Developer technologies | Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-09-02T07:37:16.633+00:00

    Hi @K, Arivalagan ,

    You could use a separate data source for it.

    A simpler example:

    List<string> list1 = new List<string>();  
    List<string> list2 = new List<string>();  
      
    public void ListView()  
    {  
    	InitializeComponent();  
      
    	list1.Add("a");  
    	list1.Add("b");  
    	list1.Add("c");  
      
    	this.ComboBox1.ItemsSource = list1;  
    	this.ComboBox2.ItemsSource = list1;  
    }  
    

    Handle it like this:

    private void ComboBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)  
    		{  
    			ComboBox2.SelectedIndex = -1;  
    			string tmp = ComboBox1.SelectedValue as string;//Save the selection of combobox1  
    			list2.Clear();  
    			list2.AddRange(list1);  
    			list2.Remove(tmp);  
    			ComboBox2.ItemsSource = null;  
    			ComboBox2.ItemsSource = list2;  
    		}  
    

    Output:

    237160-demo.png

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.