消除数据网格WPF中的重复值 [英] Eliminate duplicate values from the datagrid WPF
问题描述
请指导我如何从wpf数据网格视图中的一个指定列中删除重复值
i在互联网上搜索但我发现Winform编码的解决方案我没有得到任何wpf代码和iam不知道如何将winform syntex转换为WPF代码..
我尝试过:
这里winform代码:
Please guide me that how to remove duplicate values from one specified column in wpf datagrid view
i searched on internet but i found solutions for Winform coding iam didnt get any wpf codes and iam not aware that how to convert the winform syntex into WPF codes..
What I have tried:
here that winform code:
string initialnamevalue = grdUniqueNames.Rows[0].Cells[cellno].Text;
//Step 2:
for (int i = 1; i < grdUniqueNames.Rows.Count; i++)
{
if (grdUniqueNames.Rows[i].Cells[cellno].Text == initialnamevalue)
grdUniqueNames.Rows[i].Cells[cellno].Text = string.Empty;
else
initialnamevalue = grdUniqueNames.Rows[i].Cells[cellno].Text;
}
推荐答案
最简单的方法是使用MVVM模式的数据优先模型。对数据所做的更改通过数据绑定反映在UI中。这样,您可以将数据绑定到任何控件,并且将自动反映更改。下面是一个演示这个的例子:
首先,我们需要设置几个辅助类。
1.绑定系统:
The easiest way is a data-first model using the MVVM pattern. Changes made to the data are reflected in the UI via data binding. This way you can bind your data to any control and the changes will be automatically be reflected. Below is an example that demonstrates this:
First, we need to set up a couple of helper classes.
1. Binding system:
public abstract class ObservableBase : INotifyPropertyChanged
{
public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<TValue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
2.处理按钮点击事件(命令):
2. Handling the button click event (command):
public class RelayCommand<T> : ICommand
{
#region Fields
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;
#endregion
#region Constructors
public RelayCommand(Action<T> execute) : this(execute, null)
{
}
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
_execute = execute ?? throw new ArgumentNullException("execute");
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
=> _canExecute == null ? true : _canExecute((T)parameter);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
=> _execute(parameter == null ? default(T) : (T)parameter);
#endregion
}
下一个a用于保存样本数据的类:
Next a class to hold the sample data:
public class PersonModel : ObservableBase
{
private string name;
public string Name
{
get { return name; }
set { Set(ref name, value); }
}
private string age;
public string Age
{
get { return age; }
set { Set(ref age, value); }
}
}
接下来,ViewModel用于保存数据(模型)并处理按钮单击(命令)事件:
Next the ViewModel to hold the data (models) and handle the button click (command) event:
class MainViewModel
{
public MainViewModel()
{
for (int i = 0; i < 100; i++)
{
People.Add(new PersonModel
{
Name =
< span class =code-string> Person {i},
Age = rand.Next( 20 , 25 )。ToString()
});
}
ClickCommand = new RelayCommand< bool>(点击);
}
private 随机rand = new Random();
public ICommand ClickCommand { get ; }
public ObservableCollection< PersonModel>人{获取; }
= new ObservableCollection< PersonModel>();
private void 点击( bool state)
{
// remove duplelicates
foreach ( var group People.GroupBy(x = > x.Age))
{
< span class =code-keyword> var items = group .Skip( 1 ) ;
if (items.Any())
foreach ( var item in items)
item.Age = string 跨度> .Empty;
}
}
}
"Person {i}", Age = rand.Next(20, 25).ToString() }); } ClickCommand = new RelayCommand<bool>(Clicked); } private Random rand = new Random(); public ICommand ClickCommand { get; } public ObservableCollection<PersonModel> People { get; } = new ObservableCollection<PersonModel>(); private void Clicked(bool state) { //remove duplelicates foreach (var group in People.GroupBy(x => x.Age)) { var items = group.Skip(1); if (items.Any()) foreach (var item in items) item.Age = string.Empty; } } }
上面的ViewModel处理删除dupelicates。最后一件事是UI ...
The above ViewModel handles the removing of the dupelicates. The last thing is the UI...
<Window x:Class="DataGridColumnClear.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridColumnClear"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding People}" GridLinesVisibility="None"/>
<Button Padding="10,5" Margin="10" Grid.Row="1" HorizontalAlignment="Center"
Content="Remove Dupelicates" Command="{Binding ClickCommand}"/>
</Grid>
</Window>
这篇关于消除数据网格WPF中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!