首页   

C#学生管理系统课题设计

沉默小赵  ·  · 3 年前

点击蓝色字段下载应用及试用文件 提取码联系作者QQ2098693589
在这里插入图片描述 在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CSharpDemo01
{
    public partial class frmMain : Form
    {
        private string fileName = string.Empty; //定义变量保存读取文件路径的变量
        private List<string> objListStudent = new List<string>();   //定义List存储读取到的学生信息
        private List<string> objListQuery = new List<string>(); //经过查询满足条件的学生信息
        private int actionFlag = 0; //通过actionFlag值判断添加还是修改actionFlag(1)添加,actionFlag(2)修改
        public frmMain()
        {
            InitializeComponent();
            //禁用明细区域
            gboxStudentDetail.Enabled = false;
        }


        //控件事件
       
        private void dgvStudent_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvStudent.CurrentRow.Selected == false) return;
            else
            {
                string currentSNO = dgvStudent.CurrentRow.Cells[0].Value.ToString();//读取当前行的学号
                string[] currentDetail = GetStudentBySNO(currentSNO).Split(',');
                LoadDataToDetail(currentDetail[0], currentDetail[1], currentDetail[2], currentDetail[3], currentDetail[4],
                                                        currentDetail[5], currentDetail[6]);
            }
        }
        private void btnImport_Click(object sender, EventArgs e)//导入数据并展示数据
        {
            //【1】选择文件
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "csv文件(*.csv)|*.csv|TXT文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openfile.ShowDialog()==DialogResult.OK)
            {
                fileName = openfile.FileName;   //把文件的路径赋值为全局变量fileName
            }

            //【2】把文件的数据读取到List中
            try
            {
                //读取文件
                objListStudent = ReadFileToList(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取文件出现错误,具体错误如下:" + ex.Message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            //【3】把List中的数据展示在DataGridView中
            LoadDataToDataGrid(objListStudent);

            //【4】把DataGridView的第一行数据的明细展示在明细groupBox中
            string currentSNO = dgvStudent.Rows[0].Cells[0].Value.ToString();
            string[] currentDetail = GetStudentBySNO(currentSNO).Split(',');

            LoadDataToDetail(currentDetail[0], currentDetail[1], currentDetail[2], currentDetail[3], currentDetail[4],
                                                        currentDetail[5], currentDetail[6]);
        }
        private void txtQuerySNO_TextChanged(object sender, EventArgs e)//根据学号查询
        {
            //清空存储查询结果的List
            objListQuery.Clear();

            //查看当前那些满足条件并添加到QueryList中
            foreach(string item in objListStudent)
            {
                if (item.StartsWith(txtQuerySNO.Text))
                    objListQuery.Add(item);
            }
            //清空当前表格数据
            dgvStudent.Rows.Clear();
            //把查询结果展示在DataGridView中
            LoadDataToDataGrid(objListQuery);
        }
        private void txtQueryName_TextChanged(object sender, EventArgs e)//根据姓名查询
        {
            //清空存储查询结果的List
            objListQuery.Clear();

            //查看当前那些满足条件并添加到QueryList中
            foreach (string item in objListStudent)
            {
                if (item.Contains(txtQueryName.Text))
                    objListQuery.Add(item);
            }
            //清空当前表格数据
            dgvStudent.Rows.Clear();
            //把查询结果展示在DataGridView中
            LoadDataToDataGrid(objListQuery);
        }
        private void txtQueryMobile_TextChanged(object sender, EventArgs e)//根据电话号码查询
        {
            //清空存储查询结果的List
            objListQuery.Clear();

            //查看当前那些满足条件并添加到QueryList中
            foreach (string item in objListStudent)
            {
                if (item.Contains(txtQueryMobile.Text))
                    objListQuery.Add(item);
            }
            //清空当前表格数据
            dgvStudent.Rows.Clear();
            //把查询结果展示在DataGridView中
            LoadDataToDataGrid(objListQuery);
        }
        private void btnAdd_Click(object sender, EventArgs e)//添加学生信息
        {
            //添加过程:点击添加按钮-->输入数据-->判断是否有效-->提交

            //控制启用和禁用
            DisableButton();

            //让明细窗体中文本框清空
            txtSNO.Text = string.Empty;
            txtName.Text = string.Empty;
            dtpBirthday.Text = DateTime.Now.ToString();
            rbMale.Checked = true;
            txtMobile.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtHomeAddress.Text = string.Empty;

            //让学号的文本框获得焦点
            txtSNO.Focus();

            //修改actionFlag
            actionFlag = 1;
        }
        private void btnUpdate_Click(object sender, EventArgs e)//修改学生信息
        {
            //准备
            //控制按钮
            DisableButton();

            //学号不允许修改
            txtSNO.Enabled = false;

            //让学生姓名文本框获得焦点
            txtName.Focus();

            //修改ActionFlag
            actionFlag = 2;
        }
        private void btnDelete_Click(object sender, EventArgs e)//删除学生信息
        {
            if (dgvStudent.CurrentRow.Selected==false)
            {
                MessageBox.Show("删除数据前必须要选中一行", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                string info = "您确定要删除学生 【学号:" + dgvStudent.CurrentRow.Cells[0].Value.ToString() + "姓名:" +
                                                    dgvStudent.CurrentRow.Cells[1].Value.ToString() + "】信息吗?";
                DialogResult result = MessageBox.Show(info, "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    string currentStudent = GetStudentBySNO(dgvStudent.CurrentRow.Cells[0].Value.ToString());
                    foreach (string item in objListStudent)
                    {
                        if (item.Equals(currentStudent))
                        {
                            objListStudent.Remove(item);
                            MessageBox.Show("学生信息删除成功!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //把添加后的List展示
                            dgvStudent.Rows.Clear();
                            LoadDataToDataGrid(objListStudent);
                            break;
                        }
                    }
                }
                else return;
            }
        }
        private void btnCommit_Click(object sender, EventArgs e)//提交添加和修改的操作
        {
            if (!VailDate()) return;
            else
            {
                //组合数据,准备添加到List中
                string sno = txtSNO.Text.Trim();
                string sname = txtName.Text.Trim();
                string sex = rbMale.Checked == true ? "男" : "女";
                string birthday = dtpBirthday.Text;
                string mobile = txtMobile.Text;
                string email = txtEmail.Text;
                string homeAddress = txtHomeAddress.Text;

                string currentStudent = sno + ',' + sname + ',' + sex + ',' + birthday + ',' + mobile + ','
                                + email + ',' + homeAddress;
                switch (actionFlag)
                {
                    case 1://添加
                        //把数据添加到List
                        objListStudent.Add(currentStudent);
                        //把添加后的List展示
                        dgvStudent.Rows.Clear();
                        LoadDataToDataGrid(objListStudent);
                        //显示添加成功
                        MessageBox.Show("学生信息添加成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //控件按钮
                        EnableButton();
                        break;
                    case 2://修改
                        for (int i = 0; i < objListStudent.Count; i++)
                        {
                            if (objListStudent[i].StartsWith(sno))
                            {
                                //删除原有的
                                objListStudent.RemoveAt(i);
                                //插入添加的
                                objListStudent.Insert(i, currentStudent);
                                break;
                            }
                        }
                        //把添加后的List展示
                        dgvStudent.Rows.Clear();
                        LoadDataToDataGrid(objListStudent);
                        //显示修改成功
                        MessageBox.Show("学生信息修改成功", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //控件按钮
                        EnableButton();
                        //把学号文本框启用
                        txtSNO.Enabled = true;
                        break;
                    default:
                        break;
                }
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)//取消添加和修改
        {
            //控制按钮
            EnableButton();
        }
        private void btnClose_Click(object sender, EventArgs e)//退出
        {
            DialogResult result = MessageBox.Show("系统推出,是否要保存修改", "系统提示", 
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                //执行保存
                //【1】清空
                File.WriteAllText(fileName, string.Empty);

                //【2】把objList逐行写入
                StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default);
                foreach (string item in objListStudent)
                {
                    sw.WriteLine(item);
                }
                sw.Close();
                //【3】写入完成
                MessageBox.Show("写入完成!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Close();
        }


        //用户自定义方法
        private List<string> ReadFileToList(string filePath)//把某一个文件读取,并以List方式返回给调用者
        {
            List<string> objList = new List<string>();
            string line = string.Empty;
            try
            {
                StreamReader file = new StreamReader(filePath, Encoding.Default);
                while ((line = file.ReadLine()) != null)
                {
                    objList.Add(line);
                }
                file.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return objList;
        }
        private void LoadDataToDataGrid(List<string> objList)//把List中的数据展示在DataGridView中
        {
            //依次读取List中数据
            foreach (string item in objList)
            {
                string[] studentArray = item.Split(',');
                //把读取的当前学生插入到DataGirdView中
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvStudent);
                row.Cells[0].Value = studentArray[0];
                row.Cells[1].Value = studentArray[1];
                row.Cells[2].Value = studentArray[2];
                row.Cells[3].Value = studentArray[3];
                row.Cells[4].Value = studentArray[4];
                dgvStudent.Rows.Add(row);
            }
        }
        private void LoadDataToDetail(string sno, string sname, string sex, string birthday,
                            string mobile, string email, string homeAddress)//把一个学生的明细展示在明细groupBox中
        {
            txtSNO.Text = sno;
            txtName.Text = sname;
            if (sex == "男") rbMale.Checked = true;
            else rbFemale.Checked = true;
            dtpBirthday.Text = birthday;
            txtMobile.Text = mobile;
            txtEmail.Text = email;
            txtHomeAddress.Text = homeAddress;
        }
        private string GetStudentBySNO(string sno)//根据学号查到某一个学生的具体信息
        {
            string currentStudent = string.Empty;
            foreach (string item in objListStudent)
            {
                if (item.StartsWith(sno))
                {
                    currentStudent = item;
                    break;
                }
            }
            return currentStudent;
        }
        private void DisableButton()//禁用按钮
        {
            //禁用按钮
            btnAdd.Enabled = false;
            btnImport.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;

            //启用groupbox
            gboxStudentDetail.Enabled = true;
        }
        private void EnableButton()//启用按钮
        {
            //启用按钮
            btnAdd.Enabled = true;
            btnImport.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;

            //启用groupbox
            gboxStudentDetail.Enabled = false;
        }
        private bool VailDate() //用户添加和修改后数据的效验
        {
            bool b = true;
            //学号和姓名不能为空,学号不能重复(添加!)
            if (string.IsNullOrWhiteSpace(txtSNO.Text))//学号不能为空!
            {
                MessageBox.Show("学号不能为空!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSNO.Focus();
                b = false;
            }
            if (string.IsNullOrWhiteSpace(txtName.Text))//姓名不能为空!
            {
                MessageBox.Show("姓名不能为空!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                b = false;
            }
            if (actionFlag == 1)
            {
                if (txtSNO.Text.Trim()==GetStudentBySNO(txtSNO.Text.Trim()).Split(',')[0])
                {
                    MessageBox.Show("该学号已经存在,请重新输入学号!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSNO.Focus();
                    b = false;
                }
            }
            return b;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
推荐文章
半导体行业联盟  ·  ​晶圆二哥:联电设立半导体设备学院  ·  1 年前  
真叫卢俊的地产观  ·  如何拯救一个烂到渣的小三房户型  ·  7 年前  
© 2022 51好读
删除内容请联系邮箱 2879853325@qq.com