我需要知道在添加,更新或删除记录后是否有办法刷新绑定源,这样在删除记录的情况下,我将能够在绑定导航器工具栏中显示正确的记录计数
我正在制作一个薪资申请模块,目前正在添加银行。我无法添加两个连续记录(现在一次只能添加一个,关闭我的应用程序,然后添加另一个)。我的应用程序已集成到SAP Business 1。
在我的BankMasterForm.cs上我有
private void BankMaintenanceForm_Load(object sender, EventArgs e)
{
bankMasterBindingSource.DataSource = Program.Kernel.Get<IBankMasterService>().GetAllBankMasters();
}
我也在使用这个应用程序的Ninject插件
我在IBankMasterService.cs(界面)表格上的代码:
namespace Payroll.BLL
{
public interface IBankMasterService
{
IEnumerable<BankMaster> GetAllBankMasters();
BankMaster GetBankMaster(string code);
void AddBankMaster(BankMaster bankMaster);
void UpdateBankMaster(BankMaster bankMaster);
void DeleteBankMaster(BankMaster bankMaster);
string GenerateSAPCode();
}
}
我也有BankMasterService.cs类:
namespace Payroll.BLL
{
public class BankMasterService : IBankMasterService
{
private readonly IBankMasterRepository _bankMasterRepository;
public BankMasterService(
IBankMasterRepository bankMasterRepository)
{
Guard.AgainstNullParameter(bankMasterRepository, "bankMasterRepository");
_bankMasterRepository = bankMasterRepository;
}
public IEnumerable<BankMaster> GetAllBankMasters()
{
return _bankMasterRepository.SelectAll();
}
public BankMaster GetBankMaster(string code)
{
var rulesException = new RulesException<BankMaster>();
// Business rule: Answer Id cannot be less than 1
if (!code.HasValue())
rulesException.ErrorFor(x => x.Code, "Code cannot be null");
if (rulesException.Errors.Any()) throw rulesException;
return _bankMasterRepository.GetEntity(code);
}
public void AddBankMaster(BankMaster bankMaster)
{
// Business rule: 0
var errors = DataAnnotationsValidationRunner.GetErrors(bankMaster);
var rulesException = new RulesException();
rulesException.ErrorsForModel(errors);
// Other validation rules
if (rulesException.Errors.Any()) throw rulesException;
// Save to database
_bankMasterRepository.Insert(bankMaster);
}
public void UpdateBankMaster(BankMaster bankMaster)
{
// Business rule: 0
var errors = DataAnnotationsValidationRunner.GetErrors(bankMaster);
var rulesException = new RulesException();
rulesException.ErrorsForModel(errors);
// Other validation rules
if (rulesException.Errors.Any()) throw rulesException;
// Save to database
_bankMasterRepository.Update(bankMaster);
}
public void DeleteBankMaster(BankMaster bankMaster)
{
var bm = GetBankMaster(bankMaster.Code);
if (bm != null)
{
// Delete from database
_bankMasterRepository.Delete(bm);
}
}
public string GenerateSAPCode()
{
var result = default(long);
var codesList = from b in GetAllBankMasters()
select new { Code = long.Parse(b.Code) };
codesList = codesList.OrderBy(x => x.Code);
var lastRecord = codesList.LastOrDefault();
if (lastRecord != null)
result = lastRecord.Code + 1;
return result.ToString();
}
}
}
我的BankMasterForm.cs表单上的示例删除功能是:
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are You Sure You Want To Delete This Record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
// Get service instance
var bankMasterService = Program.Kernel.Get<IBankMasterService>();
var bankMasterDelete =
new BankMaster
{
Code = txt_code.Text
};
// Delete record
bankMasterService.DeleteBankMaster(bankMasterDelete);
//clear textfields after input
txt_code.Text = string.Empty;
txt_bank_code.Text = string.Empty;
txt_bank_name.Text = string.Empty;
cb_bank_type.Text = null;
txt_address1.Text = string.Empty;
txt_address2.Text = string.Empty;
txt_comments.Text = string.Empty;
MessageBox.Show("Bank Details Deleted Successfully");
}
}
我的问题是,如何获得我的绑定来源,即:
bankMasterBindingSource.DataSource = Program.Kernel.Get<IBankMasterService>().GetAllBankMasters();
例如,删除后刷新?
非常感谢任何帮助。
答案 0 :(得分:-1)
我很确定.ResetBindings()调用会强制它刷新列表。
但是,如果您正在进行GUI操作,最好使用绑定源来执行删除操作。这样您就可以使用表单等的验证属性,然后在下面实现基础结构来完成您需要完成的工作。想象一个多客户端系统,一旦列表不断刷新,就会发生许多变化。因此,绑定源允许您抽象视图更改的时间和频率,但是适合于维护具有某种程度理性的本地状态。
答案 1 :(得分:-1)
我找到了答案。只需在每次编辑或添加记录后添加此行代码。它会刷新绑定源。
// Refresh the datasource
bankMasterBindingSource.DataSource = Program.Kernel.Get<IBankMasterService>().GetAllBankMasters().ToList();
这样,系统可以“看到”新添加或更新的记录