Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
320
How to validate unique columns in xamdatagrid
posted

Hello Everyone,

I am trying to restrict user to select duplicate values in a column from xamdatagrid.

Can anyone explain me how to achieve the above requirement.

Data grid cell style is combobox so user can select items from combobox. 

Below is my Model

Employee.cs

public class Employee
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

private string _department;
public string Department
{
get
{
return _department;
}
set
{
_department = value;
}
}

public Employee(string name, string dep)
{
this.Name = name;
this.Department = dep;
}
}

ViewModel.CS

 

public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection _coll1;

public ObservableCollection Coll1
{
get
{
return _coll1;
}
set
{
_coll1 = value;
NotifyPropertyChanged("Coll1");
}
}

private ObservableCollection _coll2;

public ObservableCollection Coll2
{
get
{
return _coll2;
}
set
{
_coll2 = value;
NotifyPropertyChanged("Coll2");
}
}

private List _employeeColl;

public List EmployeeColl
{
get
{
return _employeeColl;
}
set
{
_employeeColl = value;
NotifyPropertyChanged("EmployeeColl");
}
}

public ViewModel()
{
Coll1 = new ObservableCollection () { "AAA", "AASSSA", "AAAAXX"};
Coll2 = new ObservableCollection () { "XX", "DD", "DDD" };

EmployeeColl = new List() { new Employee("Emp1", "Dep1"), new Employee("Emp2", "Dep2") };
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

Xaml

<Grid>
<igDP:XamDataGrid DataSource="{Binding Path=EmployeeColl, UpdateSourceTrigger=PropertyChanged}">
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings/>
</igDP:XamDataGrid.FieldSettings>

<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AddNewRecordLocation="OnTopFixed" AllowAddNew="True" AllowDelete="True" SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="ErrorIconAndHighlight"/>
</igDP:XamDataGrid.FieldLayoutSettings>

<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:ComboBoxField Name="Name" Width="*">
<igDP:ComboBoxField.EditorStyle>
<Style TargetType="igEditors:XamComboEditor">
<Setter Property="ItemsSource"
Value="{Binding Path=DataContext.Coll1, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" />
</Style>
</igDP:ComboBoxField.EditorStyle>
</igDP:ComboBoxField>
<igDP:ComboBoxField Name="Department" Width="*">
<igDP:ComboBoxField.EditorStyle>
<Style TargetType="igEditors:XamComboEditor">
<Setter Property="ItemsSource"
Value="{Binding Path=DataContext.Coll2, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" />
</Style>
</igDP:ComboBoxField.EditorStyle>
</igDP:ComboBoxField>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>

</Grid>

  • 34810
    Offline posted

    Hello Yugandhar,

    In order to validate uniqueness between records in a single XamDataGrid column, I would recommend utilizing the IDataErrorInfo interface and creating a method that marks a "duplicate" error for your particular data item for that particular underlying Field. The XamDataGrid supports the IDataErrorInfo interface by setting the SupportDataErrorInfo and DataErrorDisplayMode properties of the XamDataGrid.FieldLayoutSettings, which it appears you have already done. A simple example of its usage in the XamDataGrid can be found here: https://www.infragistics.com/community/forums/t/38172.aspx.

    After implementing this interface, I would recommend that you handle the EditModeEnded event on your grid. The event arguments of this event will allow you to get the Cell, and from it, its Field, its Value, and the owning Record. The owning DataRecord has a DataItem property that returns the underlying data item to that record, and so using this information, you can query your underlying data source for a duplicate-valued data item value for the property that your Field represents. If one is found, you can use the IDataErrorInfo method mentioned above to set a data error for that particular data item.

    I have attached a sample project to demonstrate the usage of the IDataErrorInfo interface to detect duplicate values. I hope this helps.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer

    XDGNoDuplicatesCase.zip