//读取学生列表中的信息到内存中 public void ReadGridViewStudents() { foreach (DataGridViewRow data in dataGridView1.Rows) { if (data.Cells[0].Value == null || data.Cells[1].Value == null || data.Cells[2].Value == null || data.Cells[0].Value.ToString() == "" || data.Cells[1].Value.ToString() == "" || data.Cells[2].Value.ToString() == "" ) { continue; } try{ Student.Sex stdSex = (data.Cells[2].Value.ToString() == "男")? Student.Sex.male: Student.Sex.female; listStudent.Add(new Student( int.Parse(data.Cells[0].Value.ToString()), data.Cells[1].Value.ToString(), stdSex)); } catch (Exception) { // } } }
public static bool Read(string prjPath, out int deskRow, out string deskColFormat, out List <Student> listStudent) { deskRow = 0; deskColFormat = ""; listStudent = new List <Student>(); FileStream fs = new FileStream(prjPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); try { br.BaseStream.Seek(0, SeekOrigin.Begin); deskRow = br.ReadInt32(); deskColFormat = br.ReadString(); int stdCount = br.ReadInt32(); for (int i = 0; i < stdCount; i++) { int id = br.ReadInt32(); string name = br.ReadString(); string sex = br.ReadString(); Student.Sex stdSex = (sex == "男")? Student.Sex.male: Student.Sex.female; int x = br.ReadInt32(); int y = br.ReadInt32(); Student std = new Student(id, name, stdSex); std.X = x; std.Y = y; listStudent.Add(std); } } catch (Exception) { return(false); } finally { br.Close(); fs.Close(); } return(true); }