private void button_update_Click(object sender, EventArgs e) { StudentInformationClass studentInformationclass = new StudentInformationClass(); studentInformationclass.NoClass = textBox_class.Text; studentInformationclass.StudentNo = textBox_student.Text; studentInformationclass.NameStudent = textBox_name.Text; studentInformationclass.Sex = radioButton1_sex.Text; studentInformationclass.BirthdayStudent = dateTimePicker1.Text; studentInformationclass.GradeStudent = Convert.ToInt32(textBox_grade.Text); frmMain.StudentsInfo[textBox_studentNo.Text].NoClass = textBox_class.Text; frmMain.StudentsInfo[textBox_studentNo.Text].NameStudent = textBox_name.Text; if (radioButton1_sex.Checked) { frmMain.StudentsInfo[textBox_studentNo.Text].Sex = radioButton1_sex.Text; } else { frmMain.StudentsInfo[textBox_studentNo.Text].Sex = radioButton2_sex.Text; } frmMain.StudentsInfo[textBox_studentNo.Text].BirthdayStudent = dateTimePicker1.Text; frmMain.StudentsInfo[textBox_studentNo.Text].GradeStudent = Convert.ToInt32(textBox_grade.Text); MessageBox.Show(this, "修改成功!" + "An element with Key =" + textBox_studentNo.Text + "has been changed", "提示", MessageBoxButtons.OK); }
private void button_insert_Click(object sender, EventArgs e) { StudentInformationClass studentInformationclass = new StudentInformationClass(); studentInformationclass.NoClass = textBox_class.Text; studentInformationclass.StudentNo = textBox_student.Text; studentInformationclass.NameStudent = textBox_name.Text; if (!radioButton1_sex.Checked && !radioButton2_sex.Checked) { MessageBox.Show(this, "请选择性别!默认为male", "提示", MessageBoxButtons.OK); radioButton1_sex.Checked = true; studentInformationclass.Sex = radioButton1_sex.Text; } else { if (radioButton1_sex.Checked) { studentInformationclass.Sex = radioButton1_sex.Text; } else { studentInformationclass.Sex = radioButton2_sex.Text; } } if (textBox_grade.Text == "") { MessageBox.Show(this, "成绩不能为空!默认为0分", "提示", MessageBoxButtons.OK); textBox_grade.Text = "0"; } else { studentInformationclass.GradeStudent = Convert.ToInt32(textBox_grade.Text); } studentInformationclass.BirthdayStudent = dateTimePicker1.Text; if ((studentInformationclass.NameStudent == "") || (studentInformationclass.StudentNo == "") || (studentInformationclass.NoClass == "")) { MessageBox.Show(this, "姓名、班级号、学号不能为空!", "提示", MessageBoxButtons.OK); } else { //利用字典添加 try { frmMain.StudentsInfo.Add(textBox_student.Text, studentInformationclass); MessageBox.Show(this, "添加成功!", "提示", MessageBoxButtons.OK); } catch (ArgumentException) { MessageBox.Show(this, "添加失败!" + "An element with Key = \"txt\" already exists.", "提示", MessageBoxButtons.OK); } } }
/// <summary> /// 读取csv文件到字典 /// </summary> /// <param name="dictionaryOpen"></param> /// <param name="path"></param> /// <returns></returns> private Dictionary <string, StudentInformationClass> OpenCsv(Dictionary <string, StudentInformationClass> dictionaryOpen, string path) { //StudentsInfo Encoding encoding = GetType(path); //Encoding.ASCII;// System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding); //记录每次读取的一行记录 string strLine = ""; //记录每行记录中的各字段内容 string[] aryLine = null; //tableHead = null; //标示是否是读取的第一行 bool IsFirst = true; //逐行读取CSV中的数据,直到结尾 while ((strLine = sr.ReadLine()) != null) { if (IsFirst == true) { //tableHead = strLine; IsFirst = false; } else { aryLine = strLine.Split(','); StudentInformationClass studentInformationclass = new StudentInformationClass(); studentInformationclass.NoClass = aryLine[0]; studentInformationclass.StudentNo = aryLine[1]; studentInformationclass.NameStudent = aryLine[2]; studentInformationclass.Sex = aryLine[3]; studentInformationclass.BirthdayStudent = aryLine[4]; studentInformationclass.GradeStudent = Convert.ToInt32(aryLine[5]); try { frmMain.StudentsInfo.Add(studentInformationclass.StudentNo, studentInformationclass); } catch (ArgumentException) { MessageBox.Show(this, "添加失败!" + "An element with Key =" + studentInformationclass.StudentNo + "already exists.", "提示", MessageBoxButtons.OK); } } } sr.Close(); fs.Close(); return(dictionaryOpen); }