點擊藍色字段下載應用及試用文件提取碼聯繫作者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;
}
}
}