Issue with Multi Select combo box in WPF ,sometimes value in combo text box shows as System.Data.DataRowView on selection of checkbox

krishna 466 Reputation points
2022-08-29T13:17:08.383+00:00

i have a multi select combo box that has a list of checkboxes and combo box has text property to search the list of items .I can select ,unselect values,search ,clear values correctly and am getting desired output of a multi select combox box .But while testing when i keep selecting combo boxes one after the another ,randomly the text in combox box search changes to System.Data.DataRowView .If i am carefully hitting only the check box then this issue does not happen ,but if i hit the label associated with that checkbox randomly this issue pops up

My code xaml file

<ComboBox    x:Name="cmbAppNameAddRemoveBranch"  Style="{StaticResource md-2s-combo }"  VerticalAlignment="top" HorizontalAlignment="Left" SelectedValuePath="Content"  SelectionChanged="CmbAppNameAddRemoveBranch_SelectionChanged"     TextBoxBase.TextChanged="CmbAppNameAddRemoveBranch_TextChanged"  IsEditable="True" IsTextSearchEnabled="True" StaysOpenOnEdit="True"   IsTextSearchCaseSensitive="False" Text=""    >  
                            <ComboBox.ItemTemplate>  
                                <DataTemplate>  
                                    <CheckBox Name="chkApp" Width="220" Checked="ChkApp_Checked"  Unchecked="ChkApp_Unchecked"  Content="{Binding APVALDESC}" IsChecked="{Binding Check_Status,Mode=TwoWay}" CommandParameter="{Binding APVALUE}"  >  
                                    </CheckBox>  
                                </DataTemplate>  
                            </ComboBox.ItemTemplate>  
                        </ComboBox>  
                        <Button x:Name="Show" Click="Show_Click"> Show selected</Button>  
                        <Button x:Name="Clear" Click="Clear_Click">Clear selected</Button>  

code behind .cs file

private void Page_Loaded(object sender, RoutedEventArgs e)  
        {  
              
            string lsErr = "";  
            cApplParameter cocAppl = new cApplParameter(Home.psProvider, Home.psconnstr, Home.psAppId1, Home.psSchema, Home.psOldSchema);  
            cocAppl.GetApplParameters("VMS3", "APPL", "", ref ldtAllApplName, ref lsErr);  
            ldtAllApplName.Columns.Add("Check_Status");  
  
            for(int lirow = 0; lirow <= ldtAllApplName.Rows.Count - 1; lirow++)  
            {  
                ldtAllApplName.Rows[lirow]["Check_Status"] = "false";  
            }  
  
            //cmbAppNameAddRemoveBranch.DisplayMemberPath = "APVALDESC";  
            //cmbAppNameAddRemoveBranch.SelectedValuePath = "APVALUE";  
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;  
        }  
  
        private void CmbAppNameAddRemoveBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
            if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")  
            {  
                cmbAppNameAddRemoveBranch.Text = "";  
                return;  
            }  
        }  
  
        private void ChkApp_Checked(object sender, RoutedEventArgs e)  
        {  
  
            CheckBox clickedBox = (CheckBox)sender;  
              
            for(int liRow = 0; liRow <= ldtAllApplName.Rows.Count - 1; liRow++)  
            {  
                if (clickedBox.Content == ldtAllApplName.Rows[liRow]["APVALDESC"].ToString().Trim())  
                {  
                    ldtAllApplName.Rows[liRow]["Check_Status"] = true;  
                }  
            }  
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;  
  
        }  
  
        private void ChkApp_Unchecked(object sender, RoutedEventArgs e)  
        {  
            CheckBox clickedBox = (CheckBox)sender;  
  
            for (int liRow = 0; liRow <= ldtAllApplName.Rows.Count - 1; liRow++)  
            {  
                if (clickedBox.Content == ldtAllApplName.Rows[liRow]["APVALDESC"].ToString().Trim())  
                {  
                    ldtAllApplName.Rows[liRow]["Check_Status"] = false;  
                }  
            }  
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;  
        }  
  
        private void Show_Click(object sender, RoutedEventArgs e)  
        {  
            string lsMessage = "";  
            DataTable pdtDetails = new DataTable();  
            pdtDetails=ldtAllApplName.Select("Check_Status='true'").CopyToDataTable();  
            for(int lirow = 0; lirow <= pdtDetails.Rows.Count - 1; lirow++)  
            {  
                lsMessage = lsMessage + pdtDetails.Rows[lirow]["APVALDESC"].ToString().Trim() + "-" +pdtDetails.Rows[lirow]["APVALUE"].ToString().Trim()+ "\n";  
                      
            }  
            MessageBox.Show(lsMessage);  
        }  
  
        private void Clear_Click(object sender, RoutedEventArgs e)  
        {  
            for (int lirow = 0; lirow <= ldtAllApplName.Rows.Count - 1; lirow++)  
            {  
                ldtAllApplName.Rows[lirow]["Check_Status"] = "false";  
            }  
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;  
            MessageBox.Show("Cleared");  
        }  
  
        private void CmbAppNameAddRemoveBranch_TextChanged(object sender, TextChangedEventArgs e)  
        {  
            try  
            {  
                if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")  
                {  
                    cmbAppNameAddRemoveBranch.Text = "";  
                    return;  
                }  
  
                DataTable ldtDetail = new DataTable();  
                if (ldtAllApplName.Select("APVALDESC LIKE '%" + cmbAppNameAddRemoveBranch.Text.ToString().Trim() + "%'").Count() > 0)  
                {  
                      
                      
                        cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.Select("APVALDESC LIKE '%" + cmbAppNameAddRemoveBranch.Text.ToString().Trim() + "%'").CopyToDataTable().DefaultView;  
                        cmbAppNameAddRemoveBranch.IsDropDownOpen = true;  
                  
                }  
                              
                  
  
            }  
            catch(Exception ex)  
            {  
                MessageBox.Show(ex.Message);  
            }  
              
        }  
    }  

i tried getting the Text value and manually set it to blank in SelectionChanged and TextChanged event ,but when i debug cmbAppNameAddRemoveBranch.Text this is already blank and condition never meets

if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")  
            {  
                cmbAppNameAddRemoveBranch.Text = "";  
                return;  
            }  
Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-08-30T06:55:18.957+00:00

    @krishna , Welcome to Microsoft Q&A, based on my test, I reproduced your problem when I double click the check box.

    You could try to use cmbAppNameAddRemoveBranch.SelectedItem.ToString() to check if the string is System.Data.DataRowView.

    Code example:

     private void cmbAppNameAddRemoveBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)  
            {  
                //Console.WriteLine(cmbAppNameAddRemoveBranch.SelectedItem.ToString());  
                if(cmbAppNameAddRemoveBranch.SelectedItem!=null)  
                {  
                    if (cmbAppNameAddRemoveBranch.SelectedItem.ToString() == "System.Data.DataRowView")  
                    {  
                        Console.WriteLine("This is " + cmbAppNameAddRemoveBranch.SelectedIndex);  
                        cmbAppNameAddRemoveBranch.Text = " ";  
                        return;  
                    }  
                }  
                  
      
            }  
       
    

    Result:

    235966-2.gif
    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and 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

  2. krishna 466 Reputation points
    2022-08-30T11:10:27.427+00:00

    Hi,
    I found this code to be working for me ,

    private void CmbAppNameAddRemoveBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    		{  
      
    			try  
    			{  
    				cmbAppNameAddRemoveBranch.SelectedValue = "";  
    				cmbAppNameAddRemoveBranch.IsDropDownOpen = true;  
      
      
    			}  
    }  
    

    In XAML i had to remove this SelectedValuePath="Content"

    I tried the code you sent @Jack J Jun but i get the same System.Data.DataRowView getting printed .In my case this abrupt behaviour is fired not on double click but when i click on the border area between two items then this selection changed event is called and value changes 236010-12345.jpg

    In my code cmbAppNameAddRemoveBranch.SelectedValue="" ,though this code feels wrong it somehow is working and doesnt print System.Data.DataRowView

    However cmbAppNameAddRemoveBranch.IsDropDownOpen = true; doesnt work in this case ,where as CmbAppNameAddRemoveBranch_TextChanged it works and drop down expands

    0 comments No comments

Your answer

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