linq 在执行CRUD(创建、读取、更新、删除)操作后,是否有刷新列表的代码

lsmepo6l  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(111)
  1. public void RefreshListView()
  2. {
  3. List<StudentModel> studentModel = studentBL.GetStudents();
  4. {
  5. dataGridView1.DataSource = studentModel;
  6. dataGridView1.Columns[1].HeaderText = "First Name";
  7. dataGridView1.Columns[2].HeaderText = "Last Name";
  8. dataGridView1.Columns[0].Visible = false;
  9. dataGridView1.Columns[6].Visible = false;
  10. dataGridView1.Columns[7].Visible = false;
  11. }
  12. }
  13. private void SaveBtn_Click(object sender, EventArgs e)
  14. {
  15. if (ValidateDataFields())
  16. {
  17. string firstName = FirstNameTextBox.Text.Trim();
  18. string lastName = LastNameTextBox.Text.Trim();
  19. string gender = GenderComboBox.Text.Trim();
  20. DateTime dobString = DobPicker.Value;
  21. string age = AgeTextBox.Text.Trim();
  22. string Class = ClassTextBox.Text.Trim();
  23. string address = AddresstextBox.Text.Trim();
  24. StudentModel newStudent = new StudentModel()
  25. {
  26. FirstName = firstName,
  27. LastName = lastName,
  28. Gender = gender,
  29. dob = dobString,
  30. Age = age,
  31. Class = Class,
  32. Address = address
  33. };
  34. if (_existingStudent != null)
  35. {
  36. _studentBL.AddOrUpdateStudent(_existingStudent.Id, newStudent);
  37. }
  38. else
  39. {
  40. _studentBL.AddOrUpdateStudent(newStudent.Id, newStudent);
  41. }
  42. StudentList studentList = new StudentList();
  43. studentList.RefreshListView();
  44. studentList.Show();
  45. this.Hide();
  46. }
  47. }

当我在C Sharp(list)中执行CRUD操作时,数据没有刷新,它显示了我在dataaccesslayer中添加的相同的默认两个数据,你能帮助解决这个错误吗?我已经更新了刷新代码和保存按钮代码,请帮助我解决这个错误

67up9zun

67up9zun1#

当使用BindingListBindingSource在切线中实现INotifyPropertyChanged时,如下所示,无需触摸DataGridView,添加或编辑的数据将显示而不需要刷新。
在下面的例子中,所有的事情都在表单中完成,而对于代码,你需要进行调整。比如我如何加载数据。

  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. namespace WinFormsApp1;
  4. public partial class NoRefreshForm : Form
  5. {
  6. private BindingList<StudentModel> _students;
  7. private BindingSource _source = new BindingSource();
  8. public NoRefreshForm()
  9. {
  10. InitializeComponent();
  11. Shown += OnShown;
  12. }
  13. private void OnShown(object sender, EventArgs e)
  14. {
  15. // add several students
  16. List<StudentModel> students = new List<StudentModel>
  17. {
  18. new StudentModel() { FirstName = "Anne", LastName = "Jones" },
  19. new StudentModel() { FirstName = "Jim", LastName = "Adams" }
  20. };
  21. _students = new BindingList<StudentModel>(students);
  22. _source.DataSource = students;
  23. dataGridView1.DataSource = _source;
  24. }
  25. private void AddButton_Click(object sender, EventArgs e)
  26. {
  27. // add a new student, will show up in the DataGridView
  28. _source.Add(new StudentModel() { FirstName = "Mike", LastName = "Smith" });
  29. }
  30. private void EditCurrentButton_Click(object sender, EventArgs e)
  31. {
  32. // get current student from DataGridView current row
  33. StudentModel student = _students[_source.Position];
  34. // update properties
  35. student.FirstName = student.FirstName + "!";
  36. student.LastName = student.LastName + "@";
  37. }
  38. }
  39. // class belongs in its only file
  40. // INotifyPropertyChanged - Notifies clients that a property value has changed.
  41. public class StudentModel : INotifyPropertyChanged
  42. {
  43. private string _firstName;
  44. private string _lastName;
  45. public string FirstName
  46. {
  47. get => _firstName;
  48. set
  49. {
  50. if (value == _firstName) return;
  51. _firstName = value;
  52. OnPropertyChanged();
  53. }
  54. }
  55. public string LastName
  56. {
  57. get => _lastName;
  58. set
  59. {
  60. if (value == _lastName) return;
  61. _lastName = value;
  62. OnPropertyChanged();
  63. }
  64. }
  65. public event PropertyChangedEventHandler PropertyChanged;
  66. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  67. {
  68. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  69. }
  70. }
展开查看全部

相关问题