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

lsmepo6l  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(91)
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中添加的相同的默认两个数据,你能帮助解决这个错误吗?我已经更新了刷新代码和保存按钮代码,请帮助我解决这个错误

67up9zun

67up9zun1#

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

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WinFormsApp1;
public partial class NoRefreshForm : Form
{
    private BindingList<StudentModel> _students;
    private BindingSource _source = new BindingSource();
    public NoRefreshForm()
    {
        InitializeComponent();
        Shown += OnShown;
    }

    private void OnShown(object sender, EventArgs e)
    {
        // add several students
        List<StudentModel> students = new List<StudentModel>
        {
            new StudentModel() { FirstName = "Anne", LastName = "Jones" },
            new StudentModel() { FirstName = "Jim", LastName = "Adams" }
        };

        _students = new BindingList<StudentModel>(students);
        _source.DataSource = students;
        dataGridView1.DataSource = _source;
    }

    private void AddButton_Click(object sender, EventArgs e)
    {
        // add a new student, will show up in the DataGridView
        _source.Add(new StudentModel() { FirstName = "Mike", LastName = "Smith" });
    }

    private void EditCurrentButton_Click(object sender, EventArgs e)
    {
        // get current student from DataGridView current row
        StudentModel student = _students[_source.Position];
        // update properties
        student.FirstName = student.FirstName + "!";
        student.LastName = student.LastName + "@";
    }
}

// class belongs in its only file
// INotifyPropertyChanged - Notifies clients that a property value has changed.
public class StudentModel : INotifyPropertyChanged
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get => _firstName;
        set
        {
            if (value == _firstName) return;
            _firstName = value;
            OnPropertyChanged();
        }
    }

    public string LastName
    {
        get => _lastName;
        set
        {
            if (value == _lastName) return;
            _lastName = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

相关问题