206 lines
5.9 KiB
C#
206 lines
5.9 KiB
C#
using JY.Inspection.Common;
|
|||
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Data;
|
||
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Reflection;
|
||
|
|
using System.Windows.Forms;
|
||
|
|
|
||
|
|
namespace JY.Inspection.Frm
|
||
|
|
{
|
||
|
|
public partial class SetForm : MetroFramework.Forms.MetroForm
|
||
|
|
{
|
||
|
|
UserHelper helper = new UserHelper("");
|
||
|
|
List<User> ListUser = new List<User>();
|
||
|
|
DataTable dt = new DataTable();
|
||
|
|
public SetForm()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
|
||
|
|
if (File.Exists("user.pt"))
|
||
|
|
{
|
||
|
|
ListUser = helper.DeSerializedUser("user.pt");
|
||
|
|
ShowUserList(ListUser);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SetForm_Load(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ShowUserList(List<User> List)
|
||
|
|
{
|
||
|
|
|
||
|
|
List<User> UpList = new List<User>();
|
||
|
|
for (int i = 0; i < List.Count; i++)
|
||
|
|
{
|
||
|
|
if (i>0)
|
||
|
|
{
|
||
|
|
UpList.Add(List[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
dgvManger.DataSource = null;
|
||
|
|
dgvManger.DataSource = UpList;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
///
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="collection"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static DataTable ToDataTable<T>(IEnumerable<T> collection)
|
||
|
|
{
|
||
|
|
var props = typeof(T).GetProperties();
|
||
|
|
var dt = new DataTable();
|
||
|
|
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
|
||
|
|
if (collection.Count() > 0)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < collection.Count(); i++)
|
||
|
|
{
|
||
|
|
ArrayList tempList = new ArrayList();
|
||
|
|
foreach (PropertyInfo pi in props)
|
||
|
|
{
|
||
|
|
object obj = pi.GetValue(collection.ElementAt(i), null);
|
||
|
|
tempList.Add(obj);
|
||
|
|
}
|
||
|
|
object[] array = tempList.ToArray();
|
||
|
|
dt.LoadDataRow(array, true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return dt;
|
||
|
|
}
|
||
|
|
|
||
|
|
private User GetUser(List<User> list)
|
||
|
|
{
|
||
|
|
if (txtUser.Text == string.Empty | txtPassWord.Text == string.Empty)
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
User user = new User();
|
||
|
|
user.Index = list[list.Count - 1].Index + 1;
|
||
|
|
user.UserName = txtUser.Text;
|
||
|
|
user.PassWord = txtPassWord.Text;
|
||
|
|
switch (cmbLevel.SelectedIndex)
|
||
|
|
{
|
||
|
|
case 0:
|
||
|
|
user.Level = Autuority.管理员;
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
user.Level = Autuority.工程师;
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
user.Level = Autuority.操作员;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
return user;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取表格选中行单元格数据
|
||
|
|
/// </summary>
|
||
|
|
private void SetUser()
|
||
|
|
{
|
||
|
|
DataGridViewSelectedRowCollection rowCollection = dgvManger.SelectedRows;
|
||
|
|
if (rowCollection.Count == 0)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
DataGridViewRow row = rowCollection[0];
|
||
|
|
txtUser.Text = row.Cells[1].Value.ToString();
|
||
|
|
txtPassWord.Text = row.Cells[2].Value.ToString();
|
||
|
|
switch (row.Cells[3].Value.ToString())
|
||
|
|
{
|
||
|
|
case "管理员":
|
||
|
|
cmbLevel.SelectedIndex = 0;
|
||
|
|
break;
|
||
|
|
case "工程师":
|
||
|
|
cmbLevel.SelectedIndex = 1;
|
||
|
|
break;
|
||
|
|
case "操作员":
|
||
|
|
cmbLevel.SelectedIndex = 2;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 添加用户
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
private void btAdd_Click(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
User user = GetUser(ListUser);
|
||
|
|
var res = helper.AddUser("user.pt", ListUser, user);
|
||
|
|
if (res)
|
||
|
|
{
|
||
|
|
MessageBox.Show("添加成功");
|
||
|
|
ShowUserList(ListUser);
|
||
|
|
}
|
||
|
|
else if (user == null)
|
||
|
|
{
|
||
|
|
MessageBox.Show("添加内容为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
else
|
||
|
|
{
|
||
|
|
MessageBox.Show("添加失败");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 删除用户
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
private void btDelete_Click(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
|
||
|
|
var res = helper.DeleteUser("user.pt", ListUser, txtUser.Text);
|
||
|
|
if (res)
|
||
|
|
{
|
||
|
|
MessageBox.Show("删除成功");
|
||
|
|
ShowUserList(ListUser);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
MessageBox.Show("删除失败");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void dgvManger_SelectionChanged(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
SetUser();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 编辑用户
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
private void btEdit_Click(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
User user = GetUser(ListUser);
|
||
|
|
var res = helper.EditUser("user.pt", ListUser, user);
|
||
|
|
if (res)
|
||
|
|
{
|
||
|
|
MessageBox.Show("修改成功");
|
||
|
|
ShowUserList(ListUser);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
MessageBox.Show("修改失败");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|