Search code examples
c#wpfdatagrid

Display pairs while binding from database


Is there some way to check Column NAME for same name and if there are same name Customers = Duplicates, leave both visible. Is it possible with some kind of filter?

Getting data from database:

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                ProgressBar.IsIndeterminate = true;

                DataGrid1.ItemsSource = await GetDataAsync();

                ProgressBar.IsIndeterminate = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        }

Solution

  • Maybe when you populate your grid

    DataGrid1.ItemsSource = await GetDataAsync();
    

    you can do something like

    var gridView = await GetDataAsync();
    var collectionView = new ListCollectionView(gridView) as ICollectionView;
    collectionView.Filter = (r) => gridView.where(t => t.Name == r.Name && t != r).Count() >= 2;
    
    DataGrid1.ItemsSource = collectionView;
    

    It's only an idea. There are still problems with this:

    • This LinQ expression: gridView.where(t => t.Name == r.Name && t == r) is clearly wrong, but it gives you the idea of what you should do. Find out the correct types and the correct way to check the names.
    • You still need to do this every time you update your DataView.

    This should render only the lines that are present two times or more.

    I hope I was helpful