I have a DGV with rows of records and a toolbar with buttons to add, edit, delete, print, etc. Whether a button is enabled or not depends on the record. For example, closed invoices can't be edited or deleted. To handle this, I find the binding source PositionChanged event very convenient.
// very simple example
private void SecretarialRequestsBindingSource_PositionChanged(object sender, EventArgs e)
{
var bs = sender as BindingSource;
var drv = bs.Current as DataRowView;
if (drv is null)
{
toolAdd.Enabled = true;
toolDelete.Enabled = false;
toolEdit.Enabled = false;
}
else
{
toolAdd.Enabled = true;
toolEdit.Enabled = drv.Row.Field<int?>("Started") is null;
toolDelete.Enabled = toolEdit.Enabled;
}
}But the PositionChanged event is not triggered for the first record, that is, when the binding source position goes from null to zero. So what I've been doing is this, in the Load event. For what it's worth, there's no threading in this application.
private void SecretarialRequestsList_Load(object sender, EventArgs e)
{
// code to fill adapters, etc.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = secretarialRequestsBindingSource;
var x = new EventArgs();
SecretarialRequestsBindingSource_PositionChanged(secretarialRequestsBindingSource, x);
}
This works fine, but I'm concerned whether it's a good practice or not, and whether there's a good alternative.
How do you tailor toolbar buttons (or menus) according to the current data, especially the first record?