点击蓝色字段下载应用及试用文件提取码联系作者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;
}
}
}