private void bindingSource_CurrentChanged(object sender, EventArgs e) { DatabaseSet.使用者Row row = this.SelectedRow; btnEdit.Enabled = (row != null); if (row != null) { btnDel.Enabled = !_reservedUsername.Contains(row.帳號.ToLower()); } }
private void btnDel_Click(object sender, EventArgs e) { try { DatabaseSet.使用者Row row = this.SelectedRow; if (row != null) { if (MessageBox.Show("確定刪除使用者 " + row.帳號, "刪除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { row.Delete(); 使用者TableAdapter.Instance.Update(row); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnStore_Click(object sender, EventArgs e) { try { string username = txtUsername.Text = txtUsername.Text.Trim(); string password = txtPassword.Text = txtPassword.Text.Trim(); int authority = this.SelectedAuthority; try { if (authority == -1) { throw new SWLHMSException("請選擇使用者身分"); } if (this.EditState == EditStateType.New && _reservedUsername.Contains(username.ToLower())) { throw new SWLHMSException("登入名稱不可為關鍵字 \"admin\", \"manager\", \"ganger\", \"qa\", \"guest\""); } if (this.EditState == EditStateType.Edit) { if (bindingSource.Current != null) { txtPassword.DataBindings[0].WriteValue(); txtUsername.DataBindings[0].WriteValue(); cbxAuthority.DataBindings[0].WriteValue(); bindingSource.EndEdit(); DatabaseSet.使用者Row row = this.SelectedRow; //row.帳號 = username; //row.密碼 = password; //row.權限 = authority; int a = 使用者TableAdapter.Instance.Update(row); } } else if (this.EditState == EditStateType.New) { DatabaseSet.使用者Row newRow = this.Table.New使用者Row(); newRow.帳號 = username; newRow.密碼 = password; newRow.限 = authority; this.Table.Rows.Add(newRow); 使用者TableAdapter.Instance.Update(newRow); } this.EditState = EditStateType.None; this.UpdateIdentities(); } catch (ConstraintException) { MessageBox.Show("已存在登入名稱 \"" + username + "\",請指定其他名稱"); } } catch (Exception ex) { Global.ShowError(ex); } finally { if (this.EditState == EditStateType.Edit) { txtPassword.DataBindings[0].ReadValue(); txtUsername.DataBindings[0].ReadValue(); cbxAuthority.DataBindings[0].ReadValue(); } } }