How to loop all controls in raddataentry

3 Answers 552 Views
DataEntry
Stephen
Top achievements
Rank 1
Iron
Iron
Stephen asked on 15 Jul 2021, 11:04 PM

I have a rad data entry in a form that when I change the data through it's bindings it doesn't clear all the fields.  Usually check boxes and dates or dropdowns leave their current selected value if the new data set has null values.

!. is there a way to fix that?

2. If not how can I loop through all the controls in the rad data entry to clear all the controls?

Below only shows 4 controls instead of the many that textboxes, etc.  *Rde is the name of the rad data entry item

foreach (Control c in Rde.Controls)
{
MessageBox.Show(c.Name);
}

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Stephen
Top achievements
Rank 1
Iron
Iron
answered on 22 Jul 2021, 08:46 PM
So, I'm trying to find a certain record in the drop down.  I've used for a RadDropDownList.FindStringExact(string) but that isn't available for the commandbardropdownlist.  Is there other similar functionality I can use to find a specific item?
Stephen
Top achievements
Rank 1
Iron
Iron
commented on 22 Jul 2021, 08:49 PM

Nevermind, I just set the dropdownlist text to what I want and it worked.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 23 Jul 2021, 09:43 AM

Hi, Stephen,

I am glad that you have found a suitable solution for your case. As to the FindStringExact method, feel free to use it in the following way:

            CommandBarDropDownList dropDown = new CommandBarDropDownList();
            dropDown.DisplayMember = "LastName";
            dropDown.ValueMember = "LastName";
            dropDown.DataSource = bindingSource1;

            this.radBindingNavigator1.BindingNavigatorElement.FirstTopStripElement.Items.Add(dropDown);

            int index = dropDown.DropDownListElement.FindStringExact("Bush");
            dropDown.SelectedIndex = index;

Should you have further questions please let me know.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 23 Jul 2021, 03:42 PM

Thanks, that worked better.

This worked also to loop through all the controls:
foreach (Control cntrl in raddataentry.PanelContainer.Controls)
{
if (cntrl.Name.Contains("radPanel"))
{
foreach (Control c in cntrl.Controls)
{
// Code to clear values
}
}
}
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Jul 2021, 01:20 PM
Hello, Stephen,
 
According to the provided information, it is not clear how exactly you changed the data displayed in RadDataEntry. However, I suppose that you are setting the DataSource property. 

The internal implementation in RadDataEntry for the DataSource property clears all already generated editor controls and generates new editors according to the properties available in the assigned object. Here is the relevant code for this:
        /// <summary>
        /// Gets or sets the data source.
        /// </summary>
        /// <value>The data source.</value>
        public object DataSource
        {
            get { return this.dataSource; }
            set
            {
                if (this.dataSource != value)
                {
                    if (value != null)
                    {
                        this.dataSource = value;

                        if (this.DataEntryControl.BindingContext != null)
                        {
                            this.manager = this.DataEntryControl.BindingContext[this.DataSource];
                        }

                        if (!this.DataEntryControl.IsInitializing)
                        {
                            if (this.DataEntryControl.BindingContext != null)
                            {
                                this.Bind();
                            }
                            else
                            {
                                bindOnBindingContextChange = true;
                            }
                        }
                    }
                    else
                    {
                        this.dataSource = value;
                        this.Clear();
                    }
                }
            }
        }
        /// <summary>
        /// Binds this instance.
        /// </summary>
        public void Bind()
        {
            this.Clear();
            this.InitializeDataEntry();
            this.FindRequiredProperties();
            this.ArrangeControls();
        }
        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            if (this.designerHost != null)
            {
                foreach (Control control in new ArrayList(this.DataEntryControl.Controls))
                {
                    this.designerHost.DestroyComponent(control);
                }
            }

            this.DataEntryControl.PanelContainer.Controls.Clear();
            this.editableProperties.Clear();
            this.allReadyGeneratedProperties.Clear();
            this.controlsInEachColumn.Clear();
            this.validationInfoForEachEditor.Clear();
        }

Hence, there is no need to clear the editors as they are cleared.

I have prepared a sample project for your reference which result is illustrated in the attached gif file. Please give it a try and see how it works on your end.

In case you are still experiencing any further difficulties, it would be greatly appreciated if you can specify the exact steps how to reproduce the problem. Thus, we would be able to investigate the precise case and provide further assistance. Thank you in advance.

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 16 Jul 2021, 10:01 PM

I set the raddataentry datasource = BsrcEmp
I have an entityframework 6.0 entity named employees

To set the first record:
ObjEmp = ClsEmployees.GetById(MstEnt, decimal.Parse(RmccbEmp.SelectedValue.ToString()));
if (ObjEmp != null)
{
BsrcEmp.DataSource = ObjEmp;
}

Then when getting a new employee
// new employee
ObjEmp = new EMPLOYEES();
// add new employee to entity
MstEnt.eEMPLOYEES.Add(ObjEmp);
// Save entity
ClsCommon.SaveMstEnt(MstEnt, "RempfEmp.cs", "RbtnNew_Click");

BsrcEmp.DataSource = ObjEmp;
Nadya | Tech Support Engineer
Telerik team
commented on 20 Jul 2021, 11:58 AM

Hello, Stephen,

According to the provided code snippet, it seems that you first set the DataSource to an employee. Then, create a new employee and add it to your employee's collection. You set again the DataSource to the newly added employee. The data entry is expected to show the data relevant for the new employee. I have tested this on my side and it is working as expected. Please refer to the attached gif file:

private void radButton1_Click(object sender, EventArgs e)
{
    person = new Person(111, "Ann", "Berret", new DateTime(2021,8,1));
    this.radDataEntry1.DataSource = person;
}

Note, when you set the RadDataEntry.DataSource property, all already existing editors are cleared and new ones are automatically regenerated according to the fields that are available in the DataSource object. Please refer to the following article: https://docs.telerik.com/devtools/winforms/controls/dataentry/getting-started 

In case this is not the exact case that you have and you are experiencing further difficulties, I would kindly ask you to provide more information about the exact setup that you have so that we can reproduce the problem. Feel free to submit a support ticket from your Telerik account where you can attach a project where the problem occurs. 

I hope this helps. If you need any further assistance do not hesitate to contact us.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 20 Jul 2021, 07:18 PM

So, yes, when I set the rde.datasource to the new employee instead of settings the bindingsource datasource to the new employee it does clear all values. However, the big problem is that it then removes all the formatting of the fields that I have. All fields regardless of data type are just textboxes and all are the same width, etc.

On the form.designer.cs page I have all the formatting of the fields, but it gets changed when I change the rde.datasource. This is why I change the rde.bindingsource.datasource instead of the rde.datdasource.

How could I keep the formatting after changing the rde.datasource?

I think this is the version of telerik that I have, about a year old (2020.1.218.40).
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Jul 2021, 06:58 AM
Hello, Stephen,
 
Thank you for the provided additional information about the precise case.

I would recommend you to consider using RadDataEntry in combination with RadBindingNavigator. Thus, you can set the RadDataEntry.DataSource to the whole employees collections and iterate the different records with the binding navigator:

https://docs.telerik.com/devtools/winforms/controls/bindingnavigator/getting-started

https://docs.telerik.com/devtools/winforms/controls/dataentry/getting-started#binding-raddataentry-to-collection-from-objects 

If you need to change the automatically generated editor in RadDataEntry, please have a look at the demonstrated approach in the following help article:

https://docs.telerik.com/devtools/winforms/controls/dataentry/how-to/change-auto-generated-editor 

I believe that the above suggestions would fit your scenario and improve the user experience. Please give them a try and see how it works for your scenario.

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Principal
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/.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 22 Jul 2021, 12:20 AM

Is there some functionality with the RBNavigator that I could put in a drop down so the user can select a name from the list instead of having to keep clicking through each item to find the one they want?
Dess | Tech Support Engineer, Principal
Telerik team
commented on 22 Jul 2021, 09:55 AM

Hello, Stephen,
 
RadBindingNavigator is actually a derivative of RadCommandBar. Hence, you can add a CommandBarDropDownList populated with the employees. 

I have prepared a sample code snippet for your reference: 
        public Form1()
        {
            InitializeComponent();

            BindingSource bindingSource1 = new BindingSource();
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { FirstName = "Sarah", LastName = "Blake", Occupation = "Supplied Manager", StartingDate = new DateTime(2005, 04, 12), IsMarried = true, Salary = 3500, Gender = Gender.Female });
            employees.Add(new Employee() { FirstName = "Jane", LastName = "Simpson", Occupation = "Security", StartingDate = new DateTime(2008, 12, 03), IsMarried = true, Salary = 2000, Gender = Gender.Female });
            employees.Add(new Employee() { FirstName = "John", LastName = "Peterson", Occupation = "Consultant", StartingDate = new DateTime(2005, 04, 12), IsMarried = false, Salary = 2600, Gender = Gender.Male });
            employees.Add(new Employee() { FirstName = "Peter", LastName = "Bush", Occupation = "Cashier", StartingDate = new DateTime(2005, 04, 12), IsMarried = true, Salary = 2300, Gender = Gender.Male });
            bindingSource1.DataSource = employees;
            this.radBindingNavigator1.BindingSource = bindingSource1;
            this.radDataEntry1.DataSource = bindingSource1;

            CommandBarDropDownList dropDown = new CommandBarDropDownList();
            dropDown.DisplayMember = "LastName";
            dropDown.ValueMember = "LastName";
            dropDown.DataSource = bindingSource1;

            this.radBindingNavigator1.BindingNavigatorElement.FirstTopStripElement.Items.Add(dropDown);
        }

        private class Employee
        {
            public string FirstName
            {
                get;
                set;
            }
            public string LastName
            {
                get;
                set;
            }
            public string Occupation
            {
                get;
                set;
            }
            public DateTime StartingDate
            {
                get;
                set;
            }
            public bool IsMarried
            {
                get;
                set;
            }
            public int Salary
            {
                get;
                set;
            }
            public Gender Gender
            {
                get;
                set;
            }
        }
        private enum Gender
        {
            Female,
            Male
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 
john
Top achievements
Rank 1
commented on 21 Aug 2022, 10:58 AM

I'm therefore looking for a certain record in the pull down. For a RadDropDownList, I utilised. FindStringExact(string), however the commandbardropdownlist does not support that. Can I find a particular item using any other similar functionality?

foreach (Control cntrl in raddataentry.PanelContainer.Controls)
{
if (cntrl.Name.Contains("radPanel"))
{
foreach (Control c in cntrl.Controls)
{
// Code to clear values
}
}
}

Dess | Tech Support Engineer, Principal
Telerik team
commented on 22 Aug 2022, 02:36 PM

The FindStringExact method is available for the ListElement. Hence, you can use it in RadCommandBar as follows:

            var itemIndex = this.commandBarDropDownList1.ListElement.FindStringExact("ListItem 5");
            this.commandBarDropDownList1.SelectedIndex = itemIndex;

 

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