点击蓝色字段下载应用及试用文件
提取码联系作者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>();
private List<string> objListQuery = new List<string>();
private int actionFlag = 0;
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)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "csv文件(*.csv)|*.csv|TXT文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openfile.ShowDialog()==DialogResult.OK)
{
fileName = openfile.FileName;
}
try
{
objListStudent = ReadFileToList(fileName);
}
catch (Exception ex)
{
MessageBox.Show("读取文件出现错误,具体错误如下:" + ex.Message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
LoadDataToDataGrid(objListStudent);
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)
{
objListQuery.Clear();
foreach(string item in objListStudent)
{
if (item.StartsWith(txtQuerySNO.Text))
objListQuery.Add(item);
}
dgvStudent.Rows.Clear();
LoadDataToDataGrid(objListQuery);
}
private void txtQueryName_TextChanged(object sender, EventArgs e)
{
objListQuery.Clear();
foreach (string item in objListStudent)
{
if (item.Contains(txtQueryName.Text))
objListQuery.Add(item);
}
dgvStudent.Rows.Clear();
LoadDataToDataGrid(objListQuery);
}
private void txtQueryMobile_TextChanged(object sender, EventArgs e)
{
objListQuery.Clear();
foreach (string item in objListStudent)
{
if (item.Contains(txtQueryMobile.Text))
objListQuery.Add(item);
}
dgvStudent.Rows.Clear();
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 = 1;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
DisableButton();
txtSNO.Enabled = false;
txtName.Focus();
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);
dgvStudent.Rows.Clear();
LoadDataToDataGrid(objListStudent);
break;
}
}
}
else return;
}
}
private void btnCommit_Click(object sender, EventArgs e)
{
if (!VailDate()) return;
else
{
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://添加
objListStudent.Add(currentStudent);
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;
}
}
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)
{
File.WriteAllText(fileName, string.Empty);
StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default);
foreach (string item in objListStudent)
{
sw.WriteLine(item);
}
sw.Close();
MessageBox.Show("写入完成!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Close();
}
private List<string> ReadFileToList(string filePath)
{
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)
{
foreach (string item in objList)
{
string[] studentArray = item.Split(',');
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)
{
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;
gboxStudentDetail.Enabled = true;
}
private void EnableButton()
{
btnAdd.Enabled = true;
btnImport.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
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