承接上一篇「新增功能」表單,本文解析對應的編輯表單 SM_EditFunction。主要差異在於:建構子接收現有實體 FunctionTable editFuncItem 進行初始化、確認時執行 UPDATE 而非 INSERT、並提供 IsEdit 屬性供呼叫端判斷使用者是否真的修改了資料。
public SM_EditFunction(FunctionTable editFuncItem, ClientDictionary[] dic_funcType)
{
InitializeComponent();
oriFuncItem = editFuncItem; // 保存原始資料供更新時參考
DIC_funcType = dic_funcType;
// 綁定類別下拉選單
cbo_Type.DataSource = DIC_funcType;
cbo_Type.ValueMember = "valueMember";
cbo_Type.DisplayMember = "displayMember";
// 填入既有值
txt_Name.Text = oriFuncItem.funcName;
txt_Tag.Text = oriFuncItem.funcTag;
ckb_IsInterface.Checked = (oriFuncItem.funcIsInterface == "1");
cbo_Type.SelectedValue = oriFuncItem.funcType;
txt_FunEntity.Text = oriFuncItem.funcEntity;
txt_BelongTo.Text = oriFuncItem.funcBelongTo;
txt_Detail.Text = oriFuncItem.funcDetail;
funcTableConn = new DataAccessObject().Connection;
}
private void btn_Confirm_Click(object sender, EventArgs e)
{
// 必填驗證(同新增邏輯)
if (string.IsNullOrEmpty(txt_Name.Text)) { MessageBox.Show("功能名稱不能為空"); return; }
if (string.IsNullOrEmpty(txt_Tag.Text)) { MessageBox.Show("功能標識不能為空"); return; }
if (string.IsNullOrEmpty(txt_FunEntity.Text)) { MessageBox.Show("功能體不能為空"); return; }
newFuncItem = new FunctionTable
{
funcName = txt_Name.Text.Trim(),
funcTag = txt_Tag.Text.Trim(),
funcIsInterface = ckb_IsInterface.Checked ? "1" : "0",
funcType = cbo_Type.SelectedValue.ToString().Trim(),
funcEntity = txt_FunEntity.Text.Trim(),
funcBelongTo = txt_BelongTo.Text.Trim(),
funcDetail = txt_Detail.Text.Trim(),
// 保留原始不可編輯欄位
funcIsExpand = oriFuncItem.funcIsExpand,
funcVer = oriFuncItem.funcVer,
funcID = oriFuncItem.funcID,
funcFatherID = oriFuncItem.funcFatherID
};
UpdateFuncItem(newFuncItem);
isEdit = true; // 標記已修改
this.Close();
}
private void UpdateFuncItem(FunctionTable newItem)
{
funcTableConn.Open();
using (var tran = funcTableConn.BeginTransaction())
{
try
{
var sql = @"UPDATE 功能 SET
功能標識=:tag, 功能名稱=:name, 功能簡述=:detail,
所屬窗體=:belong, 有無界面=:interface, 功能類別=:type,
功能體=:entity, 上級功能ID=:father, 可否展顯=:expand, 版本號=:ver
WHERE 功能ID = :id";
using (var cmd = new OracleCommand(sql, funcTableConn, tran))
{
cmd.Parameters.Add("tag", newItem.funcTag);
cmd.Parameters.Add("name", newItem.funcName);
cmd.Parameters.Add("detail", newItem.funcDetail);
cmd.Parameters.Add("belong", newItem.funcBelongTo);
cmd.Parameters.Add("interface", newItem.funcIsInterface);
cmd.Parameters.Add("type", newItem.funcType);
cmd.Parameters.Add("entity", newItem.funcEntity);
cmd.Parameters.Add("father", newItem.funcFatherID);
cmd.Parameters.Add("expand", newItem.funcIsExpand);
cmd.Parameters.Add("ver", newItem.funcVer);
cmd.Parameters.Add("id", newItem.funcID);
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch (Exception ex)
{
try { tran.Rollback(); } catch { }
MessageBox.Show("修改失敗:" + ex.Message);
}
finally { funcTableConn.Close(); }
}
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
isEdit = false; // 明確標記未修改
this.Close();
}
public bool IsEdit { get { return isEdit; } } // 唯讀屬性供外部檢查
public FunctionTable NewFuncItem { get { return newFuncItem; } }
oriFuncItem 保存未修改前的值,更新時只更新使用者可編輯的欄位,funcID、funcFatherID、funcVer 等系統欄位保持不變。isEdit 讓呼叫端(如主視窗的 TreeView 節點重新整理邏輯)知道是否需重新載入資料。