【机房重构】-dataGridView 控件应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luckystar_99/article/details/85245631

前言

机房重构在完成完成了注册、充值、退卡、激活等窗体之后,突然发现操作员对卡的操作和管理完全可以通过 dataGridView 控件应用在一个窗体上实现。于是就尝试写了一下,虽然这样一来,有些功能就重复了,不过没关系,开心就好。

学生卡管理窗体

在这里插入图片描述

dataGridView 的常用属性

  • DisplayedCells 调整列宽,以适合当前屏幕上显示的行和列中的所有单元格的内容,包括标题单元格。
    在这里插入图片描述
  • DataGridViewCellStyle 标题列的字体大小在这里面设置
  • DefaultCellStyle 非标题列的字体大小在这里面设置

dataGridView 的部分功能

  • 单击列头或列中的单元格就可以选择整列
  • dataGridView添加右键菜单
  • dataGridView 只读
  • 获取被选中行某一个单元格的值

部分功能代码

private void StuCardRecord_UI_Load(object sender, EventArgs e)
        {
            #region 显示全部学生卡记录
            Facade.StuCardRecord_Facade cardfact = new Facade.StuCardRecord_Facade();
            List<Entity.StuCard_Info> list = cardfact.ShowAllStuCard();
            dataGridView1.DataSource = list;
            #endregion

            #region 单击列头或列中的单元格就可以选择整列       
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            #endregion           
            
            dataGridView1.ReadOnly = true;//只读
        }
        
        #region 选中了某一行,右击菜单
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (dataGridView1.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dataGridView1.SelectedRows.Count == 1)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }
        #endregion            

        #region 退卡
        
        private void BackCard_Click(object sender, EventArgs e)
        {
            //获取被选中行的卡号           
            int a = dataGridView1.CurrentRow.Index;
            int b = Convert.ToInt32(dataGridView1.Rows[a].Cells[0].Value);

            if (Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value) =="已退卡")
            {
                MessageBox.Show("此卡已经退卡");
            }
            else if (Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[8].Value) == "使用")
            {
                MessageBox.Show("此卡正在上机,请先下机在退卡!");
            }
            else
            {
                #region 更新学生卡-余额为0,已退卡

                Entity.StuCard_Info stuCard = new Entity.StuCard_Info();
                Facade.BackCard_Facade backCard = new Facade.BackCard_Facade();

                stuCard.StuID = Convert.ToInt32(b);
                stuCard.IsBackcard = "已退卡";
                stuCard.Balance = Convert.ToDecimal("0");

                int table = backCard.UpdateBCstuCard(stuCard);
                
                #endregion

                #region 删除用户表
                Entity.User_Info Userinfo = new Entity.User_Info();

                Userinfo.UserID = Convert.ToInt32(b);

                int dt1 = backCard.DeleteUserID(Userinfo);
                #endregion

                #region 添加退卡表
                Entity.BackCard_Info backcardinfo = new Entity.BackCard_Info();
                                
                backcardinfo.stuID = Convert.ToInt32(b);
                backcardinfo.stuName = Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value);
                backcardinfo.backMoney = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value);
                backcardinfo.backTime = DateTime.Now;
                backcardinfo.Head = Common_UI.userid;
                backcardinfo.isCheck = "未结账";

                int backTB = backCard.AddBackCard(backcardinfo);

                if (backTB != 0)
                {
                    MessageBox.Show("退卡成功!");
                }
                else
                {
                    MessageBox.Show("退卡失败!");
                }
                #endregion
            }           
        }
        #endregion

        #region 充值
        private void Recharge_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value) == "已退卡")
            {
                MessageBox.Show("此卡已经退卡,无法充值!");
            }
            else
            {
                //获取被选中行的卡号           
                int a = dataGridView1.CurrentRow.Index;
                int b = Convert.ToInt32(dataGridView1.Rows[a].Cells[0].Value);

                Recharge_UI frm = new Recharge_UI();
                frm.Show();
                Recharge_UI.txtstuID.Text = Convert.ToString(b);
            }            
        }
        #endregion

        #region 激活
        private void ReActivate_Click(object sender, EventArgs e)
        {
            MessageBox.Show("激活!");
        }
        #endregion

        #region 更新显示全部学生卡记录
        private void btnUpdate_Click(object sender, EventArgs e)
        {          
            Facade.StuCardRecord_Facade cardfact = new Facade.StuCardRecord_Facade();

            List<Entity.StuCard_Info> list = cardfact.ShowAllStuCard();

            dataGridView1.DataSource = list;          
        }
        #endregion`[
](https://www.cnblogs.com/lgx5/p/7041289.html)`

        #region 退出
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion        

后记

站在巨人的肩膀上完成了这些功能的集成,以上代码部分还可以再优化,dataGridView 更加优秀和强大的功能还等待着我们去学习。

查阅博客集结号
C#中datagridviewz中SelectionMode的四个属性的含义
DataGridView添加右键菜单等技巧
C# WinForm开发系列 - DataGridView 使用方法集锦
如何使datagridview一部分列只读
获取或设置当前单元格的内容
机房重构 DataGridView内容更新到数据库中


猜你喜欢

转载自blog.csdn.net/luckystar_99/article/details/85245631