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
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.
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
}
}
}
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.
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;
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.
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).
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
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/.
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'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
}
}
}
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;