Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / sysSetting / CtrlUser.cs
yangqianqian on 29 Dec 2020 13 KB first
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using IOM_cs.irisDb.service;
using IOM_cs.irisDb;

namespace IOM_cs.insertForm.sysSetting
{
    public partial class CtrlUser : UserControl
    {

        IUserService iUserService = (IUserService)DbService.getEntity("IUserService");
        public static CtrlUser ctrlUser;

        int totalCount = 0;
        int currentPage = 1;
        int pageCount = 0;
        int limitCount = 10;


        public CtrlUser()
        {
            InitializeComponent();

            ctrlUser = this;

            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();
            

            DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
            columncb.HeaderText = "";
            columncb.Name = "cb_check";
            columncb.TrueValue = true;
            columncb.FalseValue = false;
            columncb.HeaderText = "#";
            columncb.DataPropertyName = "IsChecked";
            dataGridView1.Columns.Add(columncb);
            dataGridView1.Columns["cb_check"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[0].Resizable = DataGridViewTriState.False;

            
            Panel panel = new Panel();
            panel.BackColor = Color.White;
            panel.Size = new System.Drawing.Size(848, 40);
            panel.Location = new Point(0, 0);
            panel.BackColor = ColorTranslator.FromHtml("#ECEEF2");
            dataGridView1.Controls.Add(panel);

            //ckBox = new CheckBox();
            //ckBox.Text = "";
            //ckBox.Checked = false;
            //System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            //ckBox.Size = new System.Drawing.Size(13, 13);
            //ckBox.BackColor = Color.White;
            //ckBox.Location = new Point(13, 13);
            //ckBox.Click += new EventHandler(ckBox_CheckedChanged);
            //dataGridView1.Controls.Add(ckBox);
            //ckBox.BringToFront();

            Label col1 = new Label();
            col1.Text = "序号";
            col1.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col1.ForeColor = ColorTranslator.FromHtml("#333333");
            col1.Location = new Point(48, 11);
            dataGridView1.Controls.Add(col1);
            col1.BringToFront();

            Label col2 = new Label();
            col2.Text = "账号";
            col2.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col2.ForeColor = ColorTranslator.FromHtml("#333333");
            col2.Location = new Point(134, 11);
            dataGridView1.Controls.Add(col2);
            col2.BringToFront();

            Label col3 = new Label();
            col3.Text = "姓名";
            col3.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col3.ForeColor = ColorTranslator.FromHtml("#333333");
            col3.Location = new Point(345, 11);
            dataGridView1.Controls.Add(col3);
            col3.BringToFront();

            //Label col4 = new Label();
            //col4.Text = "警务编号";
            //col4.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            //col4.ForeColor = ColorTranslator.FromHtml("#333333");
            //col4.Location = new Point(365, 11);
            //dataGridView1.Controls.Add(col4);
            //col4.BringToFront();

            Label col5 = new Label();
            col5.Text = "身份证号";
            col5.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col5.ForeColor = ColorTranslator.FromHtml("#333333");
            col5.Location = new Point(513, 11);
            dataGridView1.Controls.Add(col5);
            col5.BringToFront();

            Label col6 = new Label();
            col6.Text = "添加时间";
            col6.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold);
            col6.ForeColor = ColorTranslator.FromHtml("#333333");
            col6.Location = new Point(692, 11);
            dataGridView1.Controls.Add(col6);
            col6.BringToFront();
            
            totalCount = 0;
            currentPage = 1;
            pageCount = 0;
            limitCount = 10;

            LoadData();
        }

        private void CtrlUser_Load(object sender, EventArgs e)
        {
            //LoadData();
        }

        // 全选事件
        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            if (((CheckBox)sender).CheckState == CheckState.Checked)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = true;
                }
            }
            else
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    checkBox.Value = false;
                }
            }
        }

        private void initDataGridView()
        {
            DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn();
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[7].Visible = false;
            dataGridView1.Columns[4].Visible = false;

            dataGridView1.Columns[1].HeaderText = "序号";
            dataGridView1.Columns[2].HeaderText = "账号";
            dataGridView1.Columns[3].HeaderText = "姓名";
            dataGridView1.Columns[4].HeaderText = "警务编号";
            dataGridView1.Columns[5].HeaderText = "身份证号";
            dataGridView1.Columns[6].HeaderText = "添加时间";

            dataGridView1.Columns[6].DefaultCellStyle.Format = "yyyy/MM/dd HH:mm:ss";
            
            //820
            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[1].Width = 60;
            dataGridView1.Columns[2].Width = 260;
            dataGridView1.Columns[3].Width = 200;
            dataGridView1.Columns[4].Width = 140;
            dataGridView1.Columns[5].Width = 200;
            dataGridView1.Columns[6].Width = 180;

            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;
            dataGridView1.Columns[4].ReadOnly = true;
            dataGridView1.Columns[5].ReadOnly = true;
            dataGridView1.Columns[6].ReadOnly = true;
        }

        private void LoadData()
        {
            DataTable table = iUserService.GetUsers(limitCount,currentPage);
            totalCount = iUserService.GetCount();
            pageCount = totalCount/limitCount;
            int ys = totalCount % limitCount;
            if (ys != 0)
                pageCount ++;
            setPageLabel();
            dataGridView1.DataSource = table;
            initDataGridView();
            selectIndex = -1;
        }
       
        private void setPageLabel()
        {
            label1.Text = "共"+totalCount+"条,每页"+limitCount+"条,共"+pageCount+"页";
            textBoxPage.Text = currentPage.ToString();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton1_Click(object sender, EventArgs e)
        {
            clickAdd();
        }
        public void clickAdd()
        {
            CtrlUserAdd ctrlUserAdd = new CtrlUserAdd();
            if (ctrlUserAdd.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton4_Click(object sender, EventArgs e)
        {
            clickDelete();
        }
        public void clickDelete()
        {
            if (selectIndex < 0)
            {
                MessageBox.Show("请选择要删除的项", "提示");
                return;
            }

            if (MessageBox.Show("是否要删除所选项?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                DataGridViewRow row = dataGridView1.Rows[selectIndex];
                try
                {
                    Int64 id = Convert.ToInt64(row.Cells["id"].Value);
                    if (iUserService.Delete(id) == 0)
                    {
                        LoadData();
                        MessageBox.Show("数据删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("数据删除失败", "提示");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据删除失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        int selectIndex = -1;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxCell))
            {
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value = false;
                }
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                selectIndex = e.RowIndex;
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton2_Click(object sender, EventArgs e)
        {
            clickEdit();
        }
        public void clickEdit()
        {
            if (selectIndex < 0)
            {
                MessageBox.Show("请选择要编辑的项", "提示");
                return;
            }
            try
            {
                DataGridViewRow row = dataGridView1.Rows[selectIndex];

                Int64 id = Convert.ToInt64(row.Cells["id"].Value);
                String userId = row.Cells["user_id"].Value.ToString();
                String name = row.Cells["name"].Value.ToString();
                String userNumber = row.Cells["user_number"].Value.ToString();

                CtrlUserEdit userEdit = new CtrlUserEdit(id, userId, name,userNumber);
                if (userEdit.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("用户信息保存成功", "提示");
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("属性编辑失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roundButton3_Click(object sender, EventArgs e)
        {
            clickReset();
        }
        public void clickReset()
        {
            if (selectIndex < 0)
            {
                MessageBox.Show("请选中要重置的项", "提示");
                return;
            }
            if (MessageBox.Show("是否要重置密码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                DataGridViewRow row = dataGridView1.Rows[selectIndex];
                try
                {
                    Int64 id = Convert.ToInt64(row.Cells["id"].Value);
                    if (iUserService.reSetPassword(id) == 0)
                    {
                        LoadData();
                        MessageBox.Show("密码重置成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("密码重置失败", "提示");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("密码重置失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        
        private void buttonNext_Click_1(object sender, EventArgs e)
        {
            if (currentPage < pageCount)
            {
                currentPage++;
                LoadData();
            }
        }

        private void buttonPre_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                LoadData();
            }
        }       
      
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.ClearSelection();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 60, e.RowBounds.Location.Y + 12);

        }
    }
}