This is a migrated thread and some comments may be shown as answers.

Manually trigger item validation

7 Answers 805 Views
DataEntry
This is a migrated thread and some comments may be shown as answers.
Nicklas
Top achievements
Rank 1
Veteran
Nicklas asked on 06 Jan 2021, 10:43 AM

Hi, I'm using below code to validate items in the data entry. This event triggers whenever the items lose focus.

 

dataEntry.ItemValidated += (s, e) =>
                    {
                        ContactEntries employee = dataEntry.CurrentObject as ContactEntries;
                        if (e.Label.Text == "Contact person:")
                        {
                            if (employee.contactPerson.Length < 10)
                            {
                                Console.WriteLine("validated");
                                e.ErrorProvider.SetError(s as Control, "Field is required");
                                if (!dataEntry.ValidationPanel.PanelContainer.Controls.ContainsKey("Contact person:"))
                                {
                                    RadLabel label = new RadLabel();
                                    label.Name = "contactPerson";
                                    label.Text = "<html><size=10><b><color= Red>Contact person: </b><color= Black>First Name should be between 2 and 15 chars long.";
                                    label.Dock = DockStyle.Top;
                                    label.AutoSize = false;
                                    label.BackColor = Color.Transparent;
                                    dataEntry.ValidationPanel.PanelContainer.Controls.Add(label);
                                }
                            }
                            else
                            {
                                e.ErrorProvider.Clear();
                                dataEntry.ValidationPanel.PanelContainer.Controls.RemoveByKey("contactPerson");
                            }
                        }
                    };

 

On the first page of the data entry, the items contains predefined values, but the data entry is also connection with a binding navigator, allowing users to add a new page. How can I validate these two pages in case the user does not click each item that will trigger the validation?

 

Is there some way I can trigger the ItemValidated event manually? 

 

Thanks in advance!

7 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Jan 2021, 08:27 AM
Hello, Nicklas,   

The ItemValidating/ItemValidated events are purposed to be fired when an editor item loses focus and you navigate to another field. Internally, RadDataEntry subscribes to the standard Validating/Validated events for each editor control that it contains and they actually trigger the ItemValidating/ItemValidated events. There is no straight forward way that I can suggest to force triggering the ItemValidating/ItemValidated events except focusing and unfocusing the respective editors.

If you want to validate the values when you navigate to the next/previous record by using the binding navigator, I would recommend you to use the BindingSource.CurrentChanged event. The BindingSource.Current property gives you access to the current object. I have prepared a sample code snippet for your reference. Please give this approach a try and see how it works for your scenario:
        public RadForm1()
        {
            InitializeComponent();

            BindingList<Item> items = new BindingList<Item>();
            items.Add(new Item(1, "Ana Smith"));

            BindingSource bs = new BindingSource();
            bs.DataSource = items;

            this.radDataEntry1.DataSource = bs;
            this.radBindingNavigator1.BindingSource = bs;

            this.radDataEntry1.ItemValidating += radDataEntry1_ItemValidating;
            this.radDataEntry1.ItemValidated += radDataEntry1_ItemValidated;
             
            bs.CurrentChanged += bs_CurrentChanged;
            lastCurrent = bs.Current as Item;
        }

        Item lastCurrent = null;

        private void bs_CurrentChanged(object sender, EventArgs e)
        {
            Console.WriteLine(lastCurrent.Id + " " + lastCurrent.Name);
            lastCurrent = ((BindingSource)sender).Current as Item;
        }
      
        private void radDataEntry1_ItemValidating(object sender, ItemValidatingEventArgs e)
        {
        }

        private void radDataEntry1_ItemValidated(object sender, ItemValidatedEventArgs e)
        {
        }

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public Item()
            {
                this.Id = -1;
                this.Name = "";
            }

            public Item(int id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Nicklas
Top achievements
Rank 1
Veteran
answered on 07 Jan 2021, 10:47 AM

Hi Dess,

 

Could you please provide an example of how I can focus / unfocus all of the editors in all the pages (tabs)?

 

Thanks in advance

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Jan 2021, 09:04 AM
Hello, Nicklas,   

It is necessary to iterate the controls and focus each of them to force the ItemValidating event:
            foreach (RadPanel panel in this.radDataEntry1.PanelContainer.Controls)
            {
                foreach (Control item in panel.Controls)
                {
                    item.Focus();
                }
            }

I believe that it would fit your scenario.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Nicklas
Top achievements
Rank 1
Veteran
answered on 09 Jan 2021, 02:55 PM

Hi Dess,

 

Is there a way I can do this for all pages?

page 2.png
page 1 (predefined values).png

0
Nicklas
Top achievements
Rank 1
Veteran
answered on 09 Jan 2021, 03:35 PM

 

foreach (RadPanel panel in this.radDataEntry1.PanelContainer.Controls)
           {
               foreach (Control item in panel.Controls)
               {
                   item.Focus();
               }
           }

 

Above code only iterate the current panel. Am I missing something?

0
Nicklas
Top achievements
Rank 1
Veteran
answered on 12 Jan 2021, 12:25 PM
Any news?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Jan 2021, 09:58 AM

Hello, Nicklas,   

In order to force the ItemValidating event for all the "pages", you need to force navigating to the next record in the BindingSource. This will reload the controls inside the PanelContainer and then you can iterate again the Controls collection in the already discussed manner: 

        BindingSource bs = new BindingSource();
        public RadForm1()
        {
            InitializeComponent();

            BindingList<Item> items = new BindingList<Item>();
            items.Add(new Item(1, "Ana Smith"));
            items.Add(new Item(2, "John Brown"));
            items.Add(new Item(3, "David Scott"));

            bs.DataSource = items;

            this.radDataEntry1.DataSource = bs;
            this.radBindingNavigator1.BindingSource = bs;

            this.radDataEntry1.ItemValidating += radDataEntry1_ItemValidating;  
        }
         
        private void radDataEntry1_ItemValidating(object sender, ItemValidatingEventArgs e)
        {
            RadTextBox tb = sender as RadTextBox;
            if (tb!=null)
            {
                Console.WriteLine(tb.Text);
            } 
        }
         

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public Item()
            {
                this.Id = -1;
                this.Name = "";
            }

            public Item(int id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            bs.MoveFirst();

            while (  bs.Position<  bs.Count )
            { 
                foreach (RadPanel panel in this.radDataEntry1.PanelContainer.Controls)
                {
                    foreach (Control item in panel.Controls)
                    {
                        item.Focus();
                    }
                }

                if (bs.Position == bs.Count - 1)
                {
                    break;
                }
                bs.MoveNext();
            }
        }
Off topic, please have in mind that most of the forum threads are reviewed by Telerik representatives and sometimes we address the questions asked by our customers in the forums as well where the response time is 72 hours. However, a post in the forum doesn't guarantee you a response from the Telerik support team. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

Thank you for your understanding.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DataEntry
Asked by
Nicklas
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Nicklas
Top achievements
Rank 1
Veteran
Share this question
or