Note that there are some explanatory texts on larger screens.

plurals
  1. POwpf Datagrid: throw duplicated entry
    primarykey
    data
    text
    <p>In my application I want to validate if the user enter an item which is already exist on the DataGrid when I enter a new Item on the cell. I validate my business object using <code>IDataErrorInfo</code>.</p> <p>My object is as follows:</p> <pre><code> class clsProducts : INotifyPropertyChanged, IDataErrorInfo { private string _ProductName; private decimal _PurchaseRate; private int _AvailableQty; private int _Qty; private decimal _Amount; #region Property Getters and Setters public string ProductName { get { return _ProductName; } set { if (_ProductName != value) { _ProductName = value; OnPropertyChanged("ProductName"); } } } public decimal PurchaseRate { get { return _PurchaseRate; } set { _PurchaseRate = value; OnPropertyChanged("PurchaseRate"); } } public int AvailableQty { get { return _AvailableQty; } set { _AvailableQty = value; OnPropertyChanged("AvailableQty"); } } public int Qty { get { return _Qty; } set { _Qty = value; this._Amount = this._Qty * this._PurchaseRate; OnPropertyChanged("Qty"); OnPropertyChanged("Amount"); } } public decimal Amount { get { return _Amount; } set { _Amount = value; OnPropertyChanged("Amount"); } } #endregion #region IDataErrorInfo Members public string Error { get { StringBuilder error = new StringBuilder(); // iterate over all of the properties // of this object - aggregating any validation errors PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor prop in props) { string propertyError = this[prop.Name]; if (!string.IsNullOrEmpty(propertyError)) { error.Append((error.Length != 0 ? ", " : "") + propertyError); } } return error.ToString(); } } public string this[string name] { get { string result = null; if (name == "ProductName") { if (this._ProductName != null) { int count = Global.ItemExist(this._ProductName); if (count == 0) { result = "Invalid Product "+this._ProductName; } } } else if (name == "Qty") { if (this._Qty &gt; this._AvailableQty) { result = "Qty must be less than Available Qty . Avaialble Qty : " + this._AvailableQty; } } return result; } } #endregion #region INotifyPropertyChanged Members // Declare the event public event PropertyChangedEventHandler PropertyChanged; //// Create the OnPropertyChanged method to raise the event protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } #endregion } </code></pre> <p>My xaml is :</p> <pre><code> &lt;my:DataGrid Name="dgReceiveInventory" RowStyle="{StaticResource RowStyle}" ItemsSource="{Binding}" GotFocus="dgReceiveInventory_GotFocus" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" RowHeight="23" SelectionUnit="Cell" AutoGenerateColumns="False" Margin="12,84,10,52" BeginningEdit="dgReceiveInventory_BeginningEdit"&gt; &lt;my:DataGrid.Columns&gt; &lt;!--0-Product Column--&gt; &lt;my:DataGridTemplateColumn Header="Product Name" Width="200"&gt; &lt;my:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Style="{StaticResource TextBlockInError}" Text="{Binding ProductName,ValidatesOnDataErrors=True}" &gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/my:DataGridTemplateColumn.CellTemplate&gt; &lt;my:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox x:Name="txtbxProduct" Style="{StaticResource TextBoxInError}" Text="{Binding Path=ProductName,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True}" TextChanged="txtbxProduct_TextChanged" PreviewKeyDown="txtbxProduct_PreviewKeyDown" &gt; &lt;/TextBox&gt; &lt;/DataTemplate&gt; &lt;/my:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/my:DataGridTemplateColumn&gt; &lt;!--1-Purchase Rate Column--&gt; &lt;my:DataGridTextColumn Header="Purchase Rate" Width="100" Binding="{Binding PurchaseRate}" IsReadOnly="True"&gt;&lt;/my:DataGridTextColumn&gt; &lt;!--2-Avaialable Qty Column--&gt; &lt;my:DataGridTextColumn Header="AvailableQty" Binding="{Binding AvailableQty}" IsReadOnly="True" Visibility="Hidden"&gt;&lt;/my:DataGridTextColumn&gt; &lt;!--3-Qty Column--&gt; &lt;my:DataGridTextColumn Header="Qty" Binding="{Binding Qty,ValidatesOnExceptions=True,ValidatesOnDataErrors=True}" EditingElementStyle="{StaticResource TextBoxInError}"&gt; &lt;/my:DataGridTextColumn&gt; &lt;!--4-Amount Column--&gt; &lt;my:DataGridTextColumn Header="Amount" Width="100" Binding="{Binding Amount}" &gt;&lt;/my:DataGridTextColumn&gt; &lt;/my:DataGrid.Columns&gt; &lt;/my:DataGrid&gt; </code></pre> <p>Now I want to show the user ,if he made a duplicate entry in the datagrid cell how to do this ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload