C#机房重构——学生上机状态查看(重点是如何实现将全部删除和选中部分的数据删除)

前言

整个机房收费系统有三个不同层次的权限:一般用户,操作员,管理员。他们在系统中都有各自的权限,其中我个人认为操作员是最重要的,因为他不仅要管理下面,还要向上汇报工作,起到了桥梁的作用,承上启下。

但是如果你问我操作员最有范的权限是啥,我一定会告诉你:学习上机状态查看。从表面上看没有什么特别之处,但是你了解内部权限后就明白为什么了。他不仅有查看的权限,更重要的是他可以管理学生的上机,最帅气的是可以让全部学生下机和选中学生下机。个人认为这是操作员最牛的权限,哈哈哈。

当然今天不是炫耀这个的,我是想跟大家分享:如何让你想实现全部人员下机,如何实现选中人员下机。看似简单的功能还是稍微的费了点劲(小白)。

困难点:如何在查出来的数据表中添加标记,然后在数据库中删除添加标记的数据,并且窗体上不显示。

下面先看我对整个窗体的功能梳理,这个有利于写代码和思路的打开。

 

解决问题的重要代码

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex;
            //获取单元格行数
            rowIndex = Convert.ToInt32(e.RowIndex.ToString());
            dataGridView.Rows[rowIndex].Cells[5].Value = "√";

        }

        private void dataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowIndex;
            rowIndex = Convert.ToInt32(e.RowIndex.ToString());
            dataGridView.Rows[rowIndex].Cells[5].Value = "";
        }

流程图

重要代码

DAL层代码

 public class StuViewOnLineStatusDAO:IDAL.IStuOnLineState 
    {
        SqlHelper sqlhelper = new SqlHelper();
        public DataTable UpdateOnLine(OnLineInfo onlineinfo)
        {
            string sql = @"select * from [OnLine_Info] ";
            DataTable table = sqlhelper.ExecuteQuery(sql,CommandType .Text );
            return table;
        }

        public DataTable DeleteAllStudent(OnLineInfo onlineinfo)
        {
            string sql = @"delete from OnLine_Info ";
            DataTable  AllStudent = sqlhelper.ExecuteQuery(sql, CommandType.Text);
            return AllStudent;
        }

        public DataTable DeletePartStudent(OnLineInfo onlineinfo)
        {
            string sql = @"delete from [OnLine_Info] where cardno=@cardno ";
            SqlParameter[] sqlParams = {
                new SqlParameter("@cardno", onlineinfo.cardno )
                                       };
            DataTable PartStudent = sqlhelper.ExecuteQuery(sql,sqlParams, CommandType.Text);
            return PartStudent;
        }

    }

BLL层代码

 

public class StuViewOnLineStatusBLL
    {
        factoryStuViewOnLineStatus fact = new factoryStuViewOnLineStatus();
        public DataTable UpdateOnlineBLL(OnLineInfo onlineinfo)
        {
            IStuOnLineState idal = fact.UpdateOnLine();
            DataTable table = idal.UpdateOnLine(onlineinfo);
            return table; 
        }


        public DataTable DeleteAllStudent(OnLineInfo onlineinfo)
        {
            IStuOnLineState idal = fact.DeleteAllStudent();
            DataTable AllStudent = idal.DeleteAllStudent(onlineinfo);
            return AllStudent;
        }
        

        public DataTable DeletePartStudent(OnLineInfo onlineinfo)
        {
            IStuOnLineState idal = fact.DeletePartStudent();
            DataTable PartStudent = idal.DeletePartStudent(onlineinfo);
            return PartStudent;
        }
    }

UI层代码

public partial class frmStuViewOnLineStatus : Form
    {
        public frmStuViewOnLineStatus()
        {
            InitializeComponent();
        }

        //实例化MOdel层
        OnLineInfo onlineinfo = new OnLineInfo();
        StudentInfo studentinfo = new StudentInfo();
        LineInfo lineinfo = new LineInfo();

        //实例化外观层
        fStuViewOnLineStatus fstuviewonlinestatus = new fStuViewOnLineStatus();

        private void MeuAllShow_Click(object sender, EventArgs e)
        {

            DataTable table = fstuviewonlinestatus.UpdateOnLine(onlineinfo);

            int m = table.Rows.Count;
            if (table.Rows.Count > 0)
            {
                dataGridView.DataSource = table;
                dataGridView.Columns.Remove("cardtype");
                dataGridView.Columns.Remove("studentNo");
                dataGridView.Columns.Remove("Department");
                dataGridView.Columns.Remove("date");
                dataGridView.Columns.Remove("longtime");
                
                return;
            }
            else
            {
                MessageBox.Show("没有相关记录!", "提示");
                return;
            }
        }

        private void MeuAllDown_Click(object sender, EventArgs e)
        {
            DataTable AllStudent = fstuviewonlinestatus.DeleteAllStudent(onlineinfo);
            if (dataGridView .SelectedRows .Count ==0)
            {
                dataGridView.DataSource = "null"; 
            }
            else
            {
                MessageBox.Show("没有任何信息可供删除!", "提示");
                return;
            }
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex;
            //获取单元格行数
            rowIndex = Convert.ToInt32(e.RowIndex.ToString());
            dataGridView.Rows[rowIndex].Cells[5].Value = "√";

        }

        private void dataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowIndex;
            rowIndex = Convert.ToInt32(e.RowIndex.ToString());
            dataGridView.Rows[rowIndex].Cells[5].Value = "";
        }

        private void MeuPartDown_Click(object sender, EventArgs e)
        {
            onlineinfo.cardno = dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[1].Value.ToString();
            DataTable PartStudent = fstuviewonlinestatus.DeletePartStudent(onlineinfo);
            if (dataGridView .SelectedRows .Count ==0)
            {
                MessageBox.Show("请至少选择一行数据进行删除!");
                return;

            }
            else
            {
                dataGridView.Rows.Remove(dataGridView.Rows[dataGridView.CurrentRow.Index]);
                MessageBox.Show("删除成功!");
            }
        }

总结

不管困难有多大,不管自己有多菜,凡事一点一点的去做,去试,去思考。总会有一个让你内心惊喜的结果。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术蜗牛-阿春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值