using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace setudentmanage
{
public partial class student : Form
{
private string filename = string.Empty;
private List<string> objliststudent = new List<string>();//定义一个list存储读取到的学生信息。
public student()
{
InitializeComponent();
}
//运行过程--业务逻辑
//数据的导入和展示,选择文件》导入到表格中》展示选择的学生明细》选择数据不同明细数字随之变化
//控件事件
private void label2_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btnInport_Click(object sender, EventArgs e)
{
//【1】选择文件
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "Txt文件(*.txt)|*.txt|csv文件(*.csv)|*.csv)|所有文件(*.*)|*.*)";
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中展示到dategrewvieew
loadDateToGrewview(objliststudent);
//【4】把dategridview中第一行的数据显示在明细中
string currentSno=dgvstudent.Rows[0].Cells[0].Value.ToString();
string currentSnoDetial=getStudentbysno(currentSno);
string[] currentSnoDetailArry = currentSnoDetial.Split(',');
//把返回的显示在明细中
loadDateToDetail(currentSnoDetailArry[0], currentSnoDetailArry[1], currentSnoDetailArry[2], currentSnoDetailArry[3], currentSnoDetailArry[4], currentSnoDetailArry[5], currentSnoDetailArry[6], currentSnoDetailArry[7]);
}
private void dgvstudent_SelectionChanged(object sender, EventArgs e)
{
if (dgvstudent.CurrentRow.Selected == false) return;
else
{
string currentsno = dgvstudent.CurrentRow.Cells[0].Value.ToString();//获取鼠标当前行的学号。
string studentcurrent = getStudentbysno(currentsno);
string[] studengcurrentArry = studentcurrent.Split(',');
loadDateToDetail(studengcurrentArry[0], studengcurrentArry[1], studengcurrentArry[2], studengcurrentArry[3], studengcurrentArry[4], studengcurrentArry[5], studengcurrentArry[6], studengcurrentArry[7]);
}
}
//用户自定义方法
private List<string> ReadFileTolist(string filePath)//把某一个文件数据读取并以listf方式返回给调用者。
{
List<string> objlist = new List<string>();
string line = string.Empty;
try
{
StreamReader file = new StreamReader(filePath,Encoding.Default); //读取到的文件路径,和编码的方法两个参数
///
//while ((line = file.ReadLine()) == null)//当line不等于空
//{
//}
//
line = file.ReadLine();
while (line != null)//line不等于空
{
objlist.Add(line);
line = file.ReadLine();
}
///
file.Close();
}
catch(Exception ex)
{
throw ex;
}
return objlist;
}
private void loadDateToGrewview(List<string> objlist)//把list中的数据显示在dategridview中
{
//依次读取linst中数据
foreach(string item in objlist)
{
string[] studentArrary = item.Split(',');
//读取的当前学生插入到dategridview中
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvstudent);
row.Cells[0].Value = studentArrary[0];
row.Cells[1].Value = studentArrary[1];
row.Cells[2].Value = studentArrary[2];
row.Cells[3].Value = studentArrary[3];
row.Cells[4].Value = studentArrary[4];
//row.Cells[5].Value = studentArrary[5];
dgvstudent.Rows.Add(row);
}
}
private void loadDateToDetail(string sno,string sname,string sex,string brithday,string mobile,string email,string homeAddress,string photo)//把一个学生的明细显示在明细页中
{
textSno.Text = sno;
textName.Text = sname;
if (sex == "男")
{
rdMale.Checked = true;
}
else
{
rbFemale.Checked = true;
}
dtBrithday.Text = brithday;
textMobile.Text = mobile;
textMail.Text = email;
textHomeaddress.Text = homeAddress;
if (photo == null) return;
}
private string getStudentbysno(string sno)//根据鼠标当前行第一行的学生学号,返回学生的详细信息
{
string currentStudeng = string.Empty;
foreach (string item in objliststudent)
{
if (item.StartsWith(sno))
{
currentStudeng = item;
break;
}
}
return currentStudeng;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace setudentmanage
{
public partial class student : Form
{
private string filename = string.Empty;
private List<string> objliststudent = new List<string>();//定义一个list存储读取到的学生信息。
public student()
{
InitializeComponent();
}
//运行过程--业务逻辑
//数据的导入和展示,选择文件》导入到表格中》展示选择的学生明细》选择数据不同明细数字随之变化
//控件事件
private void label2_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btnInport_Click(object sender, EventArgs e)
{
//【1】选择文件
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "Txt文件(*.txt)|*.txt|csv文件(*.csv)|*.csv)|所有文件(*.*)|*.*)";
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中展示到dategrewvieew
loadDateToGrewview(objliststudent);
//【4】把dategridview中第一行的数据显示在明细中
string currentSno=dgvstudent.Rows[0].Cells[0].Value.ToString();
string currentSnoDetial=getStudentbysno(currentSno);
string[] currentSnoDetailArry = currentSnoDetial.Split(',');
//把返回的显示在明细中
loadDateToDetail(currentSnoDetailArry[0], currentSnoDetailArry[1], currentSnoDetailArry[2], currentSnoDetailArry[3], currentSnoDetailArry[4], currentSnoDetailArry[5], currentSnoDetailArry[6], currentSnoDetailArry[7]);
}
private void dgvstudent_SelectionChanged(object sender, EventArgs e)
{
if (dgvstudent.CurrentRow.Selected == false) return;
else
{
string currentsno = dgvstudent.CurrentRow.Cells[0].Value.ToString();//获取鼠标当前行的学号。
string studentcurrent = getStudentbysno(currentsno);
string[] studengcurrentArry = studentcurrent.Split(',');
loadDateToDetail(studengcurrentArry[0], studengcurrentArry[1], studengcurrentArry[2], studengcurrentArry[3], studengcurrentArry[4], studengcurrentArry[5], studengcurrentArry[6], studengcurrentArry[7]);
}
}
//用户自定义方法
private List<string> ReadFileTolist(string filePath)//把某一个文件数据读取并以listf方式返回给调用者。
{
List<string> objlist = new List<string>();
string line = string.Empty;
try
{
StreamReader file = new StreamReader(filePath,Encoding.Default); //读取到的文件路径,和编码的方法两个参数
///
//while ((line = file.ReadLine()) == null)//当line不等于空
//{
//}
//
line = file.ReadLine();
while (line != null)//line不等于空
{
objlist.Add(line);
line = file.ReadLine();
}
///
file.Close();
}
catch(Exception ex)
{
throw ex;
}
return objlist;
}
private void loadDateToGrewview(List<string> objlist)//把list中的数据显示在dategridview中
{
//依次读取linst中数据
foreach(string item in objlist)
{
string[] studentArrary = item.Split(',');
//读取的当前学生插入到dategridview中
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvstudent);
row.Cells[0].Value = studentArrary[0];
row.Cells[1].Value = studentArrary[1];
row.Cells[2].Value = studentArrary[2];
row.Cells[3].Value = studentArrary[3];
row.Cells[4].Value = studentArrary[4];
//row.Cells[5].Value = studentArrary[5];
dgvstudent.Rows.Add(row);
}
}
private void loadDateToDetail(string sno,string sname,string sex,string brithday,string mobile,string email,string homeAddress,string photo)//把一个学生的明细显示在明细页中
{
textSno.Text = sno;
textName.Text = sname;
if (sex == "男")
{
rdMale.Checked = true;
}
else
{
rbFemale.Checked = true;
}
dtBrithday.Text = brithday;
textMobile.Text = mobile;
textMail.Text = email;
textHomeaddress.Text = homeAddress;
if (photo == null) return;
}
private string getStudentbysno(string sno)//根据鼠标当前行第一行的学生学号,返回学生的详细信息
{
string currentStudeng = string.Empty;
foreach (string item in objliststudent)
{
if (item.StartsWith(sno))
{
currentStudeng = item;
break;
}
}
return currentStudeng;
}
}
}