- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Clearing fields that can't be cleared :)
Joined: 05-Jul-2008
SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll : 2.6.8.1013 SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll : 2.6.8.1110 SD.LLBLGen.Pro.DQE.Oracle10g.NET20.dll : 2.6.8.1009
.NET 3.5, Adapter, General2008, Oracle 9i
I have a simple sample app (attached if it fits otherwise emailed) where I am clearing the value of two comboboxes when the first combobox value has changed. All 3 of the fields the comboboxes are bound to are non-nullable.
When the form first loads up all comboboxes show up correctly as empty with error provider icons "field [x] is required". The problem has been getting the other two field values to clear once a value is set; either the combo clears and not the underlying field value, or the underlying field value and not the combo, and/or the error provider icon doesn't pop again.
I was able to get it work by the following custom method in my CommonEntityBase. However it feels a bit like I hack and I'd appreciate a sanity check to see if there is a better way?:
namespace CRMA.Model.EntityClasses
{
public partial class CommonEntityBase
{
public void SetFieldValueNull(IEntityField2 field)
{
field.BeginEdit();
field.CurrentValue = null;
field.EndEdit();
this.Validator.ValidateFieldValue(this, field.FieldIndex, null);
PostFieldValueSetAction(true, field.Name);
}
}
}
The form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CRMA.Model.HelperClasses;
using CRMA.Model.EntityClasses;
using System.Diagnostics;
using CRMA.Model;
namespace ClearReqdFieldValidation
{
public partial class BatchEditForm : Form
{
#region Declarations
private EntityCollection<BillingCentersEntity> _billingCenters
= new EntityCollection<BillingCentersEntity>();
private EntityCollection<ReviewersAndCodersLookup> _coders
= new EntityCollection<ReviewersAndCodersLookup>();
private EntityCollection<ReviewersAndCodersLookup> _reviewers
= new EntityCollection<ReviewersAndCodersLookup>();
#endregion Declarations
public BatchEditForm()
{
InitializeComponent();
//uxCoderCombo.DataBindings["SelectedValue"].DataSourceNullValue = 0;
//uxReviewerCombo.DataBindings["SelectedValue"].DataSourceNullValue = 0;
}
private void Batch_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == BatchesFields.BillingCenterId.Name)
{
ClearCoderAndReviewer();
}
OutputData();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
LoadCombosWithFakeData();
batchesBindingSource.AddNew();
this.Batch.PropertyChanged += Batch_PropertyChanged;
OutputData();
}
private void ClearCoderAndReviewer()
{
// custom method, feels like a hack
this.Batch.SetFieldValueNull(this.Batch.Fields["CoderId"]);
this.Batch.SetFieldValueNull(this.Batch.Fields["ReviewerId"]);
// won't work
//this.Batch.SetNewFieldValue("ReviewerId", null);
//this.Batch.SetNewFieldValue("CoderId", null);
// will clear combo but field value not null, no error
//this.Batch.CoderId = 0;
//this.Batch.ReviewerId = 0;
// will clear combo but field remains zero and not in error:
//uxCoderCombo.SelectedIndex = -1;
//uxReviewerCombo.SelectedIndex = -1;
// doesn't work:
//uxCoderCombo.SelectedValue = null;
//uxReviewerCombo.SelectedValue = null;
// can clear out when DataSourceNullValue is 0 but field remains zero and not in error:
//uxCoderCombo.SelectedValue = 0;
//uxReviewerCombo.SelectedValue = 0;
OutputData();
}
#region Demo Load Data Methods
private void LoadCombosWithFakeData()
{
LoadBillingCenters();
LoadCoders();
LoadReviewers();
}
private void LoadBillingCenters()
{
_billingCenters.Add(new BillingCentersEntity { Name = "Bill Ctr 1", BillingCenterId = 1 });
_billingCenters.Add(new BillingCentersEntity { Name = "Bill Ctr 2", BillingCenterId = 2 });
billingCenterBindingSource.DataSource = _billingCenters;
}
private void LoadCoders()
{
_coders.Add(new ReviewersAndCodersLookup
{
Name = "Coder 1",
RevCodeId = 1,
BillingCenterId = 1
});
_coders.Add(new ReviewersAndCodersLookup
{
Name = "Coder 2",
RevCodeId = 2,
BillingCenterId = 2
});
codersBindingSource.DataSource = _coders;
}
private void LoadReviewers()
{
_reviewers.Add(new ReviewersAndCodersLookup
{
Name = "Reviewer 1",
RevCodeId = 1,
BillingCenterId = 1
});
_reviewers.Add(new ReviewersAndCodersLookup
{
Name = "Reviewer 2",
RevCodeId = 2,
BillingCenterId = 2
});
reviewersBindingSource.DataSource = _reviewers;
}
#endregion Demo Load Data Methods
private BatchesEntity Batch
{
[DebuggerNonUserCode()]
get { return batchesBindingSource.Current as BatchesEntity; }
set { batchesBindingSource.DataSource = value; }
}
private void OutputData()
{
uxDebugTextBox.Clear();
if (null == this.Batch) return;
uxDebugTextBox.AppendText(string.Format("BillingCenterId Prop: {0}, CurrentValue: {1}{2}",
this.Batch.BillingCenterId, this.Batch.Fields["BillingCenterId"].CurrentValue, Environment.NewLine));
uxDebugTextBox.AppendText(string.Format("CoderId Prop: {0}, CurrentValue: {1}{2}",
this.Batch.CoderId, this.Batch.Fields["CoderId"].CurrentValue, Environment.NewLine));
uxDebugTextBox.AppendText(string.Format("ReviewerId Prop: {0}, CurrentValue: {1}{2}",
this.Batch.ReviewerId, this.Batch.Fields["ReviewerId"].CurrentValue, Environment.NewLine));
}
}
}
There is also a custom base validator in play; see the ValidatorClasses folder in the model project.
Thanks
I don't think that's an LLBLGen Pro issue.
Anyway I think all you have to do is insert a dummy empty or "Please select a value" item in both of the 2nd and 3rd combos, which should have an ID of 0.
Then you need to handle SelectedIndexChange of the first combo to manually reset the other combos by selecting the first item.
Joined: 05-Jul-2008
Yeah I was not suggesting it was an issue but was posing the question on how best to reset the non-nullable fields back to their original "null" / default / empty state.
In looking at the LLCOOLJ source code, I see there is a lot more going on in EntityBase2.SetValue called from SetNewFieldValue. I cannot use SetNewFieldValue as I guess validation short-circuits the process. In my SetFieldValueNull method, it seemed to be enough to Begin and End edit, directly set field.CurrentValue, reinvoke validation, and raise property changed for databinding but I could be missing something.
The combobox "null surrogate" / fake empty item solution is not ideal for me here. I use that elsewhere but (a) it creates other problems and (b) I only like to add the fake empty item if the combobox is a field that the user should be able to clear because it is optional, which is not the case here.
The combobox "null surrogate" / fake empty item solution is not ideal for me here. I use that elsewhere but (a) it creates other problems and (b) I only like to add the fake empty item if the combobox is a field that the user should be able to clear because it is optional, which is not the case here.
No problem still you should handle SelectedIndexChange of the first combo to manually reset the other combos by selecting the first item from each combo.
That's what you want to do, isn't it?
Joined: 05-Jul-2008
No. I want to reset the other combos to empty (no selection) without fake empty items in those other combos (just as they appear when you load the form initially).
Again this works using my current code that I posted in the attachment but I was not sure if it was the best way. It is not a combobox-only thing; also applies to date/time editors etc. and I may want to do it in the business tier as well. It is more of a "how do I best reset required field value back to its default value while keeping validation, databinding etc. in tact". Clearing / "nulling" / resetting a field's value seems like a pretty basic thing to me; I wanted to be sure there was nothing else built-in (eliminating need for my custom function) and if my SetFieldValueNull approach has any holes.
Thanks