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