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
875
Disallow same record entry in datapresenter
posted

With the xamdatapresenter, how can I disallow the entry of a record based on some condition?

For example, my datapresenter (bound to a collection) has Customer Last Name, First Name etc.  If i wanted to not allow the user to enter a customer with the same first and last name, how can I do this?  I'd rather not allow the record to populate and then delete it.

Parents
No Data
Reply
  • 2677
    posted

    The xam data grid will add the record to your datasource directly once you start typing into the AddNewRecord.  So, in order to check for duplicate values, we will have to handle the RecordUpdating event and make our checks there.  You will see what I did in the code snippet below.  If I find the values for both firstname and lastname, I select the record and then execute the DeleteSelectedRecords command on the grid.  Then, in the recordsDeleting event, I turn off the prompt message so you don't see it when the behind the scenes deletion occurs.  I hope this works for you.

    private void xamDataGrid1_RecordUpdating(object sender, Infragistics.Windows.DataPresenter.Events.RecordUpdatingEventArgs e)
            {
                BindingList<Person> people = this.xamDataGrid1.DataSource as BindingList<Person>;
                string firstName = e.Record.Cells["FirstName"].Value.ToString();
                string lastName = e.Record.Cells["LastName"].Value.ToString();
                bool hasFirstName = people.Count(p=>p.FirstName == firstName) > 1 ? true : false;
                bool hasLastName = people.Count(p=>p.LastName == lastName) > 1 ? true : false;
                if (hasFirstName && hasLastName)
                {
                    e.Record.IsSelected = true;
                    this.xamDataGrid1.ExecuteCommand(DataPresenterCommands.DeleteSelectedDataRecords);
                }
            }

            private void xamDataGrid1_RecordsDeleting(object sender, Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
            {
                e.DisplayPromptMessage = false;
            } 

Children
No Data