Data Binding Best Practices
Master the Beep data binding model: SetValue/GetValue, IBeepDataSource, master-detail, navigation, and validation
Table of Contents
Overview
Beep Controls use a passive data binding model. Instead of the traditional
WinForms DataBindings collection, Beep controls expose SetValue and
GetValue methods that connect to an IBeepDataSource. This gives you
full control over when data flows in and out, and integrates naturally with the UnitOfWork
change-tracking system.
The binding stack has three layers:
- IBeepDataSource — the data provider that abstracts over DataTable, List<T>, or Entity Framework.
- BaseControl.SetValue/GetValue — per-control methods to push/pull values to/from the data source.
- BeepBindingNavigator — toolbar-style navigator that moves the current record pointer.
The SetValue / GetValue Pattern
Every Beep data-editing control implements SetValue(object value) and
GetValue(). These are the entry points for the binding engine.
// Set a value into a textbox from your data source
var row = dataSource.GetCurrentRow();
nameTextBox.SetValue(row["Name"]);
ageNumericBox.SetValue(row["Age"]);
hireDatePicker.SetValue(row["HireDate"]);
// -- user edits --
// Pull values back from the controls
row["Name"] = nameTextBox.GetValue();
row["Age"] = ageNumericBox.GetValue();
row["HireDate"] = hireDatePicker.GetValue();
dataSource.UpdateCurrentRow(row);
For controls that display complex types, SetValue and GetValue
use the appropriate type conversion:
// BeepDateTimePicker handles DateTime values
myDatePicker.SetValue(DateTime.Today); // DateTime
DateTime selected = (DateTime)myDatePicker.GetValue();
// BeepComboBox can bind to display/value members
categoryCombo.SetValue("DisplayMember", "ValueMember");
var selectedId = categoryCombo.GetValue();
IBeepDataSource — The Data Contract
IBeepDataSource is the abstraction layer that allows Beep controls to work
with any data store. Implementations exist for DataTable, List, and Entity Framework.
public interface IBeepDataSource
{
object GetCurrentRow();
void UpdateCurrentRow(object row);
bool MoveNext();
bool MovePrevious();
bool MoveFirst();
bool MoveLast();
int CurrentPosition { get; set; }
int RowCount { get; }
bool HasChanges { get; }
void AcceptChanges();
void RejectChanges();
IEnumerable Validate();
}
Using a data source with a grid is straightforward:
// Bind a DataTable-backed source
IBeepDataSource source = new BeepDataTableSource(myDataTable);
grid.DataSource = source;
// Bind a list-backed source
var customers = db.Customers.ToList();
IBeepDataSource source = new BeepListSource(customers);
grid.DataSource = source;
UnitOfWork Integration
Beep's binding system integrates with UnitOfWork for atomic save/rollback.
The UnitOfWork tracks which rows were Added, Modified, or Deleted, and the data source
defers persistence until Commit() is called.
var uow = new UnitOfWork(connectionString);
var source = uow.GetDataSource();
// Bind to controls
nameTextBox.SetValue(source.GetCurrentRow().Name);
// Deferred persistence
navigator.SaveClicked += (s, e) =>
{
// Pull latest values from UI into the current row
source.GetCurrentRow().Name = nameTextBox.GetValue();
source.GetCurrentRow().Email = emailTextBox.GetValue();
uow.Commit(); // Persists all tracked changes atomically
};
// Rollback
cancelButton.Click += (s, e) =>
{
uow.Rollback();
source.Refresh();
RebindAllControls();
};
Master-Detail Binding
Master-detail scenarios link a parent data source to a child data source via a foreign key. When the user navigates to a different parent record, the child data source automatically filters to show only related rows.
var customerSource = new BeepListSource(customers);
var orderSource = new BeepListSource(allOrders);
// Link child to parent via master-detail relationship
customerSource.AddDetailSource(
detailSource: orderSource,
parentKeyField: "Id",
childForeignKeyField: "CustomerId"
);
masterGrid.DataSource = customerSource;
detailGrid.DataSource = orderSource;
// Navigating customers automatically filters orders
// No manual filter code needed
Validation Pipeline
Beep controls have a built-in validation pipeline that runs before data is committed. Validation can happen at the control level, the data source level, or both.
// Control-level validation
nameTextBox.Validating += (s, e) =>
{
var value = nameTextBox.GetValue() as string;
if (string.IsNullOrWhiteSpace(value))
{
e.ErrorMessage = "Name is required.";
e.Cancel = true;
}
};
// Data-source-level validation
source.ValidateRow = (row) =>
{
var errors = new List();
if (string.IsNullOrWhiteSpace(row.Name))
errors.Add(new ValidationError("Name", "Name is required."));
if (row.Age < 18)
errors.Add(new ValidationError("Age", "Age must be at least 18."));
return errors;
};
// Before saving, validate the entire data source
saveButton.Click += (s, e) =>
{
var errors = source.Validate();
if (errors.Any())
{
errorLabel.Text = string.Join("\n", errors.Select(e => e.Message));
return;
}
uow.Commit();
};
Custom Data-Bound Controls
To make a custom control participate in the Beep binding system, implement
IBeepBindableControl. This ensures your control can receive and
provide values to the data source.
public class RatingControl : BaseControl, IBeepBindableControl
{
private int _rating;
public object GetValue() => _rating;
public void SetValue(object value)
{
_rating = Convert.ToInt32(value);
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
// Paint star rating based on _rating
}
protected override void OnClick(EventArgs e)
{
// Allow user to click stars to set rating
Point pt = PointToClient(Cursor.Position);
_rating = pt.X / (Width / 5) + 1;
Invalidate();
}
}
Best Practices
- Always use IBeepDataSource — do not bind controls directly to DataTable or List properties; go through the abstraction.
- Pull values before saving — call
GetValue()on every bound control beforeCommit(). The data source does not auto-sync. - Validate at both levels — use control-level
Validatingfor immediate feedback and data-source-levelValidateRowfor business rules. - Use UnitOfWork with navigator — the paired UoW/navigator pattern gives you atomic save/rollback and standard CRUD buttons.
- Set master-detail relationships early — link detail sources before binding to grids to avoid extra filter calls.
- Disable controls during data load — use
BeginUpdate()/EndUpdate()to prevent UI flicker while populating bound controls. - Handle CurrentChanged — subscribe to
source.CurrentChangedto push new row data into detail controls when the user navigates.