|
- using HKLib.DB.Model.Entity;
- using HKLib.Dto;
- using HKLib.Interfaces;
- using HKLib.Logic;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace HKCardManager.UserPages
- {
- //定义委托
- public delegate void BindHandle(object sender, EventArgs e);
- public partial class PersonnelEntryPage : UserControl
- {
- //List<string> OrgList = new List<string>();
- List<OrgTable> orgTables = new List<OrgTable>();
-
- public PersonnelEntryPage()
- {
- InitializeComponent();
-
- #region 禁止更改宽高
- // 禁止用户改变DataGridView的所有列的列宽
- dataGridView1.AllowUserToResizeColumns = false;
- //禁止用户改变DataGridView所有行的行高
- dataGridView1.AllowUserToResizeRows = false;
- // 禁止用户改变列头的高度
- dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- // 禁止用户改变列头的宽度
- dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
- #endregion
-
- this.Load += PersonnelEntryPage_Load;
-
- dataGridView1.AutoGenerateColumns = false;
- //comboBox1.SelectedIndex = 0;
- orgTables = ServiceHandler.GetOrgList();
- Global.OrgList.Clear();
- orgTables?.ForEach(item =>
- {
- Global.OrgList.Add(item.Name);
- });
- comboBox2.DataSource = Global.OrgList;
- Task.Factory.StartNew(() => { GetUser(); });
- }
-
- private void GetUser()
- {
- var res = HKLibHelper.GetUserList("");
- Global.UserListDtos.Clear();
- Global.PagUserListDtos.Clear();
- res?.ToList()?.ForEach(item =>
- {
- string status = "";
- string cardNum = "";
- if (item.Cards != null && item.Cards.Count > 0)
- {
- cardNum = item.Cards.ElementAt(0).CardNum;
- status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
- }
- Global.UserListDtos.Add(new UserInfoModel()
- {
- UserName = item.Name,
- Phone = item.Phone,
- OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
- CardNum = cardNum,
- State = status
- });
- });
-
- for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
- {
- if (Global.PagUserListDtos.Count < pageSize)
- Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
- else
- break;
- }
-
-
- this.Invoke(() =>
- {
- //dataGridView1.DataSource = null; dataGridView1.DataSource = Global.UserListDtos;
- dataGridView1.DataSource = null; dataGridView1.DataSource = Global.PagUserListDtos;
- totalCount = Global.UserListDtos.Count;
- comboBoxNum();
- showDataGirdView();
- });
-
- }
-
- private void GetPag()
- {
- Global.PagUserListDtos.Clear();
- for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
- {
- if (Global.PagUserListDtos.Count < pageSize)
- Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
- else
- break;
- }
- dataGridView1.DataSource = null;
- dataGridView1.DataSource = Global.PagUserListDtos;
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(textBox1.Text.Trim()))
- {
- MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
- return;
- }
- if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
- {
- if (!textBox2.Text.Trim().IsMobile())
- {
- MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
- return;
- }
- }
- UserDto userDto = new UserDto()
- {
- Name = textBox1.Text.Trim(),
- Phone = textBox2.Text.Trim(),
- OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
- };
- string OrgName = comboBox2.Text;
- Task.Factory.StartNew(() =>
- {
- this.Invoke(() => { button2.Enabled = false; });
- if (HKLibHelper.AddUser(userDto))
- {
- GetUser();
- this.Invoke(() =>
- {
- textBox1.Text = string.Empty;
- textBox2.Text = string.Empty;
- });
- MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
- }
- else
- MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
- this.Invoke(() => { button2.Enabled = true; });
- });
-
- }
-
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- int index = dataGridView1.CurrentRow.Index;
- if (index <= dataGridView1.Rows.Count - 1 && dataGridView1.CurrentCell.Value.ToString() == "删除")
- {
- var res = dataGridView1.Rows[index].Cells[0].Value?.ToString();
- if (string.IsNullOrEmpty(res))
- {
- MessageLogNotify.GetInstance.ShowError("用户名无效");
- return;
- }
- Task.Factory.StartNew(() =>
- {
- if (HKLibHelper.DisableUser(res))
- {
- GetUser();
- MessageLogNotify.GetInstance.Show($"用户:【{res}】 删除成功");
- }
- else
- MessageLogNotify.GetInstance.ShowError($"用户:【{res}】 删除失败");
- });
- }
- }
-
- #region datagridView 分页显示
-
- #region 变量定义
- private int totalCount = 0;//总数据个数
- private int pageCount = 0;//总页数
- private int currentPageIndex = 1;//当前页索引
- private int pageSize = 10;//每页分页大小
- private int remainder = 0;//最后一页剩余个数
- private bool isAutoUpdateLabelTip = true;//获取或设置是否自动更新分页标签内容提示,默认为true
- private string sqlAddress = "";//"Data Source=192.168.100.144;Initial Catalog=test;User ID=sa;Password=123456;";//DataGridViewd要绑定的表名
- private string tableName = "";//"DataSource";//DataGridViewd要绑定的表名
- private string strWhere = "";
- private DataSet dsAll = null;//总数据源
- #endregion
- public event EventHandler CellClick;//datagridview cellclick 触发事件
-
- #region 外调函数
- /// <summary>
- /// 绑定数据
- /// </summary>
- public void dataBand()
- {
- comboBoxNum();
- showDataGirdView();
- }
- ///// <summary>
- ///// 返回当前页面数据
- ///// </summary>
- ///// <returns></returns>
- //public DataGridView outputDGV()
- //{
- // return dataGridView_show;
- //}
- #endregion
-
-
- private void PersonnelEntryPage_Load(object? sender, EventArgs e)
- {
-
- }
-
-
- //加载事件
- //private void pager1_Load(object sender, EventArgs e)
- //{
- // comboBoxNum();
- // showDataGirdView();
- //}
- #region 分页操作
- //首页
- private void label_first_Click(object sender, EventArgs e)
- {
- currentPageIndex = 1;
- showDataGirdView();
- }
- //末页
- private void label_end_Click(object sender, EventArgs e)
- {
- currentPageIndex = pageCount;
- showDataGirdView();
- }
- //上一页
- private void label_up_Click(object sender, EventArgs e)
- {
- if (currentPageIndex <= pageCount && currentPageIndex != 1)
- {
- currentPageIndex--;
- showDataGirdView();
- }
- if (isAutoUpdateLabelTip)
- {
- updateSplitPageLabelTip();
- }
- }
- //下一页
- private void label_down_Click(object sender, EventArgs e)
- {
- if (currentPageIndex < pageCount && currentPageIndex != pageCount)
- {
- currentPageIndex++;
- showDataGirdView();
- }
- if (isAutoUpdateLabelTip)
- {
- updateSplitPageLabelTip();
- }
- }
- //第几页
- private void comboBox_page_DropDownClosed(object sender, EventArgs e)
- {
- currentPageIndex = int.Parse(comboBox_page.SelectedItem.ToString());
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- //每页条数
- private void comboBox_num_DropDownClosed(object sender, EventArgs e)
- {
- pageSize = int.Parse(comboBox_num.SelectedItem.ToString());
- //初始化页码为1
- currentPageIndex = 1;
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- //跳转指定页
- private void button_go_Click(object sender, EventArgs e)
- {
- try
- {
- currentPageIndex = int.Parse(textBox_num.Text);
- }
- catch
- {
- currentPageIndex = 1;
- }
- currentPageIndex = currentPageIndex > pageCount ? pageCount : currentPageIndex;
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- //修改显示数据
- public void updateSplitPageLabelTip()
- {
- pagingShow();
- comboBoxPage();
- comboBoxNum();
- }
- //判断显示状状况
- private void pagingShow()
- {
- //共多少页
- pageCount = totalCount / pageSize;
- remainder = totalCount % pageSize;
- pageCount = remainder == 0 ? pageCount : pageCount + 1;
- if (pageCount <= 1)
- {
- label_first.Enabled = false;
- label_end.Enabled = false;
- label_up.Enabled = false;
- label_down.Enabled = false;
- comboBox_page.Enabled = false;
- textBox_num.Enabled = false;
- button_go.Enabled = false;
- }
- else
- {
- label_first.Enabled = true;
- label_end.Enabled = true;
- label_up.Enabled = true;
- label_down.Enabled = true;
- comboBox_page.Enabled = true;
- textBox_num.Enabled = true;
- button_go.Enabled = true;
- }
- //首页关闭首页,上一页
- if (currentPageIndex == 1)
- {
- label_first.Enabled = false;
- label_up.Enabled = false;
- }
- else
- {
- label_first.Enabled = true;
- label_up.Enabled = true;
- }
- //末页关闭末页,下一页
- if (currentPageIndex == pageCount)
- {
- label_end.Enabled = false;
- label_down.Enabled = false;
- }
- else
- {
- label_end.Enabled = true;
- label_down.Enabled = true;
- }
- //共多少条
- label_count.Text = "总" + totalCount + "条/" + pageCount + "页";
- textBox_num.Text = currentPageIndex.ToString();
- }
- //多少页
- private void comboBoxPage()
- {
- comboBox_page.Items.Clear();
- for (int i = 1; i <= pageCount; i++)
- {
- comboBox_page.Items.Add(i.ToString());
- }
- comboBox_page.SelectedIndex = -1;
- //当为一页,就设置设置为0
- comboBox_page.SelectedIndex = pageCount == 1 ? 0 : currentPageIndex - 1;
- }
- //每页多少条
- private void comboBoxNum()
- {
- comboBox_num.Items.Clear();
- comboBox_num.Items.Add("10");
- comboBox_num.Items.Add("20");
- comboBox_num.Items.Add("50");
- comboBox_num.Items.Add("100");
- comboBox_num.Items.Add("500");
- comboBox_num.Items.Add("1000");
- comboBox_num.SelectedIndex = 0;
- int i = 0;
- switch (pageSize)
- {
- case 10:
- i = 0;
- break;
- case 20:
- i = 1;
- break;
- case 50:
- i = 2;
- break;
- case 100:
- i = 3;
- break;
- case 500:
- i = 4;
- break;
- case 1000:
- i = 5;
- break;
- default:
- break;
- }
- comboBox_num.SelectedIndex = i;
- }
- //获取数据
- #endregion
- #region 数据处理
- //绑定数据
- private void showDataGirdView()
- {
- //DataTable dt = new DataTable();
- //if (dsAll != null)
- //{
- // dt = GetDsAll();//获取传入数据
- //}
- //else
- //{
- // dt = GetTable();//获取数据库数据
- //}
- //try
- //{
- // //绑定前台控件
- // totalCount = int.Parse(dt.Rows[0]["total"].ToString());
- //}
- //catch
- //{
- // totalCount = 1;
- //}
- GetPag();
- updateSplitPageLabelTip();
- //绑定数据
- //dataGridView_show.DataSource = dt;
- //dataGridView_show.AllowUserToAddRows = false;
- }
- /// <summary>
- /// 获取数据库分页查询
- /// </summary>
- /// <param name="sql"></param>
- /// <returns></returns>
- //private DataTable GetTable(string sql = "")
- //{
-
-
- // DataTable dt = new DataTable();
- // try
- // {
- // MySqlConnection Connection = new MySqlConnection(sqlAddress);
- // Connection.Open();
- // //分页查询
- // if (sql == "")
- // {
- // sql = " ,(SELECT COUNT(1) FROM " + tableName + ") as total ";
- // sql = "SELECT TOP " + pageSize + " * " + sql + " FROM " + tableName + " WHERE ID NOT IN(SELECT TOP " + ((currentPageIndex - 1) * pageSize) + " ID FROM " + tableName + ")";
- // }
-
- // MySqlDataAdapter adpater = new MySqlDataAdapter(sql, Connection);
- // DataSet ds = new DataSet();
- // adpater.Fill(ds);
- // adpater.Fill(dt);
- // Connection.Close();
- // }
- // catch
- // {
- // label_count.Text = "无数据";
- // }
- // return dt;
- //}
-
- /// <summary>
- /// 获取数据源分页查询
- /// </summary>
- /// <returns></returns>
- //private DataTable GetDsAll(string sql = "")
- //{
- // DataTable dt = new DataTable();
- // try
- // {
- // dt = DataSetHelper.SplitDataSet(dsAll, pageSize, currentPageIndex).Tables[0];
- // //添加一列返回总数‘total’
- // int count = dsAll.Tables[0].Rows.Count;
- // dt.Columns.Add("total", typeof(int));
- // foreach (DataRow dr in dt.Rows)
- // {
- // dr["total"] = count;
- // }
- // }
- // catch
- // {
- // label_count.Text = "无数据";
- // }
- // return dt;
- //}
- #region DataSet分页查询
- public class DataSetHelper
- {
- public static DataSet SplitDataSet(DataSet ds, int pageSize, int pageIndex)
- {
- DataSet vds = new DataSet();
- vds = ds.Clone();
- if (pageIndex < 1) pageIndex = 1;//如果小于1,取第一页
- //if ((ds.Tables[0].Rows.Count + pageSize) <= (pageSize * pageIndex)) pageIndex = 1;
- int fromIndex = pageSize * (pageIndex - 1);//开始行
- int toIndex = pageSize * pageIndex - 1; //结束行
- for (int i = fromIndex; i <= toIndex; i++)
- {
- if (i >= (ds.Tables[0].Rows.Count)) //到达这一行,退出
- break;
- vds.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
- }
- ds.Dispose();
- return vds;
- }
- /// <summary>
- /// 根据索引和pagesize返回记录
- /// </summary>
- /// <param name="dt">记录集 DataTable</param>
- /// <param name="PageIndex">当前页</param>
- /// <param name="pagesize">一页的记录数</param>
- /// <returns></returns>
- public static DataTable SplitDataTable(DataTable dt, int PageIndex, int PageSize)
- {
- if (dt == null)
- {
- return null;
- }
- if (PageIndex == 0)
- return dt;
- DataTable newdt = dt.Clone();
- //newdt.Clear();
- int rowbegin = (PageIndex - 1) * PageSize;
- int rowend = PageIndex * PageSize;
-
-
- if (rowbegin >= dt.Rows.Count)
- return newdt;
-
-
- if (rowend > dt.Rows.Count)
- rowend = dt.Rows.Count;
- for (int i = rowbegin; i <= rowend - 1; i++)
- {
- DataRow newdr = newdt.NewRow();
- DataRow dr = dt.Rows[i];
- foreach (DataColumn column in dt.Columns)
- {
- newdr[column.ColumnName] = dr[column.ColumnName];
- }
- newdt.Rows.Add(newdr);
- }
- return newdt;
- }
-
-
- public static DataTable ToDataTable(DataRow[] rows)
- {
- if (rows == null || rows.Length == 0)
- {
- return null;
- }
- DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
- foreach (DataRow row in rows)
- {
- tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
- }
- return tmp;
- }
- }
-
- private void dataGridView_show_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- if (this.CellClick != null)
- {
- //this.CellClick(this.dataGridView_show, new EventArgs());
- }
- }
-
- #endregion
-
- #endregion
-
-
-
- #endregion
-
-
- }
- }
|