呼叫ShowData()
當窗體初始化爲填充您的列表框上初始化
public Department()
{
InitializeComponent();
ShowData();
}
ShowData()
方法,其中BindingSource
,DisplayMember
和ValueMember
設置
private void ShowData()
{
using (var uow = new UnitOfWork(new SellContext()))
{
listBox1.DataSource = uow.Departments.GetAll().ToList();
listBox1.DisplayMember = "DepartmentName";
listBox1.ValueMember = "DepartmentId";
//listBox1.Invalidate();
}
}
在當一個部門將被刪除下面的實施從數據庫列表框刷新當前集合
private void button1_Click(object sender, EventArgs e)
{
try {
using (var uow = new UnitOfWork(new SellContext()))
{
int count = uow.Departments.FindDepartmentByName(txtDeptName.Text.ToString());
if (count>0)
{
MessageBox.Show("This Department Already Exists", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
else
{
department dept = new department();
dept.DepartmentName = txtDeptName.Text.ToString();
uow.Departments.Create(dept);
if (uow.Complete() > 0)
{
MessageBox.Show("New Department Created", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtDeptName.Text = "";
ShowData();
}
else
{
MessageBox.Show("Unable to add Department", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtDeptName.Text = "";
ShowData();
}
}
}
}
catch(Exception ex)
{
ex.ToString();
}
}
ShowData()和LoadData()之間有區別嗎?還是這是一個錯誤? – dwonisch
LoadData()的代碼?你沒有發佈關於這個列表框,你有 –
的問題它是ShowData(),對不起,雖然好眼睛。 – panjo