public void RefreshListView()
{
List<StudentModel> studentModel = studentBL.GetStudents();
{
dataGridView1.DataSource = studentModel;
dataGridView1.Columns[1].HeaderText = "First Name";
dataGridView1.Columns[2].HeaderText = "Last Name";
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[6].Visible = false;
dataGridView1.Columns[7].Visible = false;
}
}
private void SaveBtn_Click(object sender, EventArgs e)
{
if (ValidateDataFields())
{
string firstName = FirstNameTextBox.Text.Trim();
string lastName = LastNameTextBox.Text.Trim();
string gender = GenderComboBox.Text.Trim();
DateTime dobString = DobPicker.Value;
string age = AgeTextBox.Text.Trim();
string Class = ClassTextBox.Text.Trim();
string address = AddresstextBox.Text.Trim();
StudentModel newStudent = new StudentModel()
{
FirstName = firstName,
LastName = lastName,
Gender = gender,
dob = dobString,
Age = age,
Class = Class,
Address = address
};
if (_existingStudent != null)
{
_studentBL.AddOrUpdateStudent(_existingStudent.Id, newStudent);
}
else
{
_studentBL.AddOrUpdateStudent(newStudent.Id, newStudent);
}
StudentList studentList = new StudentList();
studentList.RefreshListView();
studentList.Show();
this.Hide();
}
}
当我在C Sharp(list)中执行CRUD操作时,数据没有刷新,它显示了我在dataaccesslayer中添加的相同的默认两个数据,你能帮助解决这个错误吗?我已经更新了刷新代码和保存按钮代码,请帮助我解决这个错误
1条答案
按热度按时间67up9zun1#
当使用BindingList和BindingSource在切线中实现INotifyPropertyChanged时,如下所示,无需触摸DataGridView,添加或编辑的数据将显示而不需要刷新。
在下面的例子中,所有的事情都在表单中完成,而对于代码,你需要进行调整。比如我如何加载数据。