C3032
2026-01-16 2ee0bae58228032e469245e784c823693cdafe6f
LB_SmartVision/Forms/Pages/UserManagementPage/UserManagementEditPage.cs
@@ -20,12 +20,10 @@
    public partial class UserManagementEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
        RunSettingPage RunSettingPage = new RunSettingPage();
        LayoutPage LayoutPage = new LayoutPage();
        CsvPage CsvPage = new CsvPage();
        public UserManagementEditPage()
        {
            Name = "UserManagementEditPage";
            Name = "UserManagementEditPage";
            Text = "用户管理设置";
            InitializeComponent();
@@ -33,10 +31,13 @@
            InitializeComboBox();
        }
        /// <summary>
        /// 表格初始化
        /// </summary>
        private void InitializeDataGridView()
        {
            // 设置DataGridView列宽
            dataGridViewUM.ColumnCount = 5;
            dataGridViewUM.ColumnCount = 4;
            int totalWidth = dataGridViewUM.ClientSize.Width;
            int columnCount = dataGridViewUM.ColumnCount;
@@ -56,10 +57,10 @@
            // 设置列标题
            dataGridViewUM.Columns[0].Name = "用户名";
            dataGridViewUM.Columns[1].Name = "密码";
            dataGridViewUM.Columns[2].Name = "姓名";
            dataGridViewUM.Columns[3].Name = "工号";
            dataGridViewUM.Columns[4].Name = "权限";
            //dataGridViewUM.Columns[1].Name = "密码";
            dataGridViewUM.Columns[1].Name = "姓名";
            dataGridViewUM.Columns[2].Name = "工号";
            dataGridViewUM.Columns[3].Name = "权限";
            dataGridViewUM.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
@@ -73,14 +74,20 @@
            dataGridViewUM.RowHeadersVisible = true;
        }
        /// <summary>
        /// 权限下拉框初始化
        /// </summary>
        private void InitializeComboBox()
        {
            // 添加权限选项
            comboBoxPermission.Items.Add("管理员");
            comboBoxPermission.Items.Add("操作员");
            foreach (var item in Enum.GetValues(typeof(UserPermission)))
            {
                comboBoxPermission.Items.Add(item.ToString());
            }
            //comboBoxPermission.Items.Add("操作员");
            // 设置默认选择项
            comboBoxPermission.SelectedIndex = 1;
            comboBoxPermission.SelectedIndex = 0;
        }
        private void ClearInputFields()
@@ -89,7 +96,7 @@
            textBoxPassword.Clear();
            textBoxName.Clear();
            textBoxEmployeeID.Clear();
            comboBoxPermission.SelectedIndex = 1;
            comboBoxPermission.SelectedIndex = 0;
            textBoxUsername.Focus(); // 将焦点设置回用户名输入框
        }
        private void btnAdd_Click(object sender, EventArgs e)
@@ -109,7 +116,7 @@
            string[] row = new string[]
            {
                textBoxUsername.Text,
                textBoxPassword.Text, // 实际应用中密码应该加密
                //textBoxPassword.Text, // 实际应用中密码应该加密
                textBoxName.Text,
                textBoxEmployeeID.Text,
                comboBoxPermission.SelectedItem.ToString()
@@ -149,5 +156,153 @@
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private int editingRowIndex = -1;
        private bool isEditingMode = false;
        private string originalButtonText = "修改";
        private void btnEdit_Click(object sender, EventArgs e)
        {
            // 单元格可编辑
            //dataGridViewUM.ReadOnly = false;
            // 第一次点击:进入修改模式
            if (!isEditingMode)
            {
                // 检查是否选择了行
                if (dataGridViewUM.SelectedRows.Count == 0)
                {
                    MessageBox.Show("请先选择要修改的行!", "提示",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                // 获取选中的行索引
                editingRowIndex = dataGridViewUM.SelectedRows[0].Index;
                // 将选中行的数据填充到TextBox中
                FillFormWithRowData(editingRowIndex);
                // 更改按钮文本
                btnEdit.Text = "完成";
                // 进入编辑模式
                isEditingMode = true;
                // 禁用添加按钮(可选)
                btnAdd.Enabled = false;
                // 设置焦点到用户名输入框
                textBoxUsername.Focus();
                // 高亮显示正在编辑的行(可选)
                dataGridViewUM.Rows[editingRowIndex].DefaultCellStyle.BackColor = Color.LightYellow;
            }
            // 第二次点击:保存修改
            else
            {
                // 验证输入
                if (string.IsNullOrWhiteSpace(textBoxUsername.Text) ||
                    string.IsNullOrWhiteSpace(textBoxName.Text) ||
                    string.IsNullOrWhiteSpace(textBoxEmployeeID.Text))
                {
                    MessageBox.Show("请填写所有必填字段!", "提示",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                // 更新DataGridView中的行数据
                UpdateRowData(editingRowIndex);
                // 恢复按钮文本
                btnEdit.Text = originalButtonText;
                // 退出编辑模式
                isEditingMode = false;
                editingRowIndex = -1;
                // 启用添加按钮(可选)
                btnAdd.Enabled = true;
                // 恢复行颜色(可选)
                dataGridViewUM.DefaultCellStyle.BackColor = Color.White;
                // 清空输入框
                ClearInputFields();
                MessageBox.Show("修改完成!", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void FillFormWithRowData(int rowIndex)
        {
            if (rowIndex >= 0 && rowIndex < dataGridViewUM.Rows.Count)
            {
                DataGridViewRow row = dataGridViewUM.Rows[rowIndex];
                // 填充用户名
                if (row.Cells[0].Value != null)
                    textBoxUsername.Text = row.Cells[0].Value.ToString();
                else
                    textBoxUsername.Text = "";
                // 填充姓名
                if (row.Cells[1].Value != null)
                    textBoxName.Text = row.Cells[1].Value.ToString();
                else
                    textBoxName.Text = "";
                // 填充工号
                if (row.Cells[2].Value != null)
                    textBoxEmployeeID.Text = row.Cells[2].Value.ToString();
                else
                    textBoxEmployeeID.Text = "";
                // 填充权限
                if (row.Cells[3].Value != null)
                {
                    string permission = row.Cells[3].Value.ToString();
                    int index = comboBoxPermission.FindString(permission);
                    if (index >= 0)
                        comboBoxPermission.SelectedIndex = index;
                    else
                        comboBoxPermission.SelectedIndex = 0;
                }
                else
                {
                    comboBoxPermission.SelectedIndex = 0;
                }
                // 密码框清空(通常不会显示密码)
                textBoxPassword.Clear();
                // 如果需要修改密码,可以添加注释或占位符
                //textBoxPassword.PlaceholderText = "如需修改密码请填写";
            }
        }
        private void UpdateRowData(int rowIndex)
        {
            if (rowIndex >= 0 && rowIndex < dataGridViewUM.Rows.Count)
            {
                DataGridViewRow row = dataGridViewUM.Rows[rowIndex];
                // 更新用户名
                row.Cells[0].Value = textBoxUsername.Text;
                // 如果密码不为空,则更新密码(实际应用中应加密)
                if (!string.IsNullOrWhiteSpace(textBoxPassword.Text))
                {
                    // 这里可以添加密码加密逻辑
                    // row.Cells[1].Value = EncryptPassword(textBoxPassword.Text);
                }
                // 更新姓名
                row.Cells[1].Value = textBoxName.Text;
                // 更新工号
                row.Cells[2].Value = textBoxEmployeeID.Text;
                // 更新权限
                row.Cells[3].Value = comboBoxPermission.SelectedItem.ToString();
            }
        }
    }
}