Files
kou_zhaoxielou_shandong/口罩泄露定制款/Form/frm_UserSetting.cs
2026-01-16 20:53:33 +08:00

81 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Sunny.UI;
using System.Data;
using System.Windows.Forms;
using .Excle;
namespace .Form
{
public partial class frm_UserSetting : UIForm
{
UserExcleHelper excleHelper = new UserExcleHelper();
DataTable dt = new DataTable();
public frm_UserSetting()
{
InitializeComponent();
dt = excleHelper.ReadUserData();
ShowData();
//在添加两列按钮
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.HeaderText = "保存";
btnColumn.Name = "btnSave";
btnColumn.Text = "保存";
//按钮显示文字
btnColumn.UseColumnTextForButtonValue = true;
//按钮事件
dgv_User.Columns.Add(btnColumn);
DataGridViewButtonColumn btnColumn2 = new DataGridViewButtonColumn();
btnColumn2.HeaderText = "删除";
btnColumn2.Name = "btnDelete";
btnColumn2.Text = "删除";
//按钮显示文字
btnColumn2.UseColumnTextForButtonValue = true;
//按钮事件
dgv_User.Columns.Add(btnColumn2);
dgv_User.Font= new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
}
//将DataTable数据显示到DataGridView控件中
private void ShowData()
{
dt = excleHelper.ReadUserData();
dgv_User.DataSource = dt;
}
private void dgv_User_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//判断单元格属性为按钮
if (dgv_User.Columns[e.ColumnIndex].Name == "btnSave")
{
//获取按下单元格所在的行数
int rowIndex = e.RowIndex;
//获取当前行数据
DataRow dr = dt.Rows[rowIndex];
//获取输入框数据
string userName = dr["用户名"].ToString();
string password = dr["密码"].ToString();
string userPower = dr["用户权限"].ToString();
excleHelper.UpdateUserData(rowIndex + 2, userName, password, userPower);
MessageBox.Show("保存成功!");
}
if (dgv_User.Columns[e.ColumnIndex].Name == "btnDelete")
{
//获取按下单元格所在的行数
int rowIndex = e.RowIndex;
//确保按下的位置不是-1行因为-1行是列头
if (rowIndex == -1)
{
return;
}
//删除当前行数据
dt.Rows[rowIndex].Delete();
excleHelper.DeleteUserData(rowIndex + 2);
MessageBox.Show("删除成功!");
ShowData();
}
}
}
}