C# 使用BindingList更新wingridview

C# 使用BindingList更新wingridview,c#,inotifypropertychanged,bindingsource,bindinglist,C#,Inotifypropertychanged,Bindingsource,Bindinglist,按下更新按钮后,我在更新WinGridView时遇到问题。我正在使用BindingList和INotifyPropertyChanged属性。仍然无法使我的gridview更新 下面是我的简短测试程序中的代码: public Form1() { InitializeComponent(); //create BindingList using(SqlConnection connection = new SqlConnection(SQL

按下更新按钮后,我在更新WinGridView时遇到问题。我正在使用BindingList和INotifyPropertyChanged属性。仍然无法使我的gridview更新

下面是我的简短测试程序中的代码:

public Form1()
    {
        InitializeComponent();

        //create BindingList
        using(SqlConnection connection = new SqlConnection(SQLconnectionString))
        {
            connection.Open();
            using(SqlCommand command = new SqlCommand(SelectCustomer, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        customerList.Add(new Customers(reader["CntId"].ToString(), reader["Name"].ToString(), reader["Surname"].ToString()));
                    }         
                }
            }
        }

        customersBindingSource = new BindingSource(customerList, null);
        ugv_contacts.DataSource = customersBindingSource;

    }

    //update button
    private void btn_UpdateCustomer_Click(object sender, EventArgs e)
    {
        using (SqlConnection conDataBase = new SqlConnection(SQLconnectionString))
        {
            try
            {
                conDataBase.Open();
                SqlCommand updateCommand = conDataBase.CreateCommand();
                updateCommand.CommandText = UpdateCustomerDet;

                updateCommand.Parameters.AddWithValue("@CntId", Customer_Id);
                updateCommand.Parameters.AddWithValue("@CntSurname", txt_surname.Text);
                updateCommand.Parameters.AddWithValue("@CntName", txt_name.Text);

                SqlDataReader myReader;
                myReader = updateCommand.ExecuteReader();

                customersBindingSource.ResetBindings(false);

            }
            catch (Exception ex) //v primeru napake se izvede to
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    public class Customers : INotifyPropertyChanged
    {
        public Customers(string id, string surname, string name)
        {
            CntId = id;
            Name = name;
            Surname = surname;
        }

        private string id;
        private string surname;
        private string name;

        public string CntId { get { return id; } set { id = value; NotifyPropertyChanged("CntId"); } }
        public string Surname { get { return surname; } set { surname = value; NotifyPropertyChanged("Surname"); } }
        public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));

        }        
    }
有人能检查一下,告诉我我做错了什么吗?我已经搜索和测试了很多stackoverflow问题,但仍然无法使我的代码正常工作


几年后,我还没有完全找到解决办法

有人能告诉我我做错了什么吗?主要问题是.ResetBindings不起作用。我不知道为什么

先谢谢你。下面是我的代码:

 public BindingSource contactSource = new BindingSource();
 public BindingList<Contact> contactList = new BindingList<Contact>();

 public Form1()
    {
        InitializeComponent();
        contactList = GetBindingList_Contacts();
        contactSource = new BindingSource(contactList, null);
        ultraGrid1.DataSource = contactSource;

    }

public BindingList<Contact> GetBindingList_Contacts()
    {
        using (SqlConnection connection = new SqlConnection(SQLconnectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(SelectCustomer, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        contactList.Add(new Contact
                            (
                            Convert.ToInt32(reader["CntId"]), reader["Name"].ToString(), reader["Surname"].ToString(), Convert.ToInt32(reader["CntAge"])
                            )
                            );
                    }
                }
            }
        }
        return new BindingList<Contact>(contactList);
    }

private void btn_Edit_Click(object sender, EventArgs e)
    {
        int row = Convert.ToInt32(ultraGrid1.ActiveRow.ListIndex);

        contactList[row].FirstName = txt_name.Text;
        contactList[row].LastName = txt_surname.Text;
        contactList[row].Age = Convert.ToInt32(txt_Age.Text);
        //function that updated DB

    }

private void btn_refresh_Click(object sender, EventArgs e)
    {
        contactSource.ResetBindings(true);
    }

public static Contact GetBL_Contacts(int CntId)
    {
        Contact CtnDetail = new Contact();
        using (SqlConnection conDataBase = new SqlConnection(SQLconnectionString))
        {
            try
            {
                conDataBase.Open();
                SqlCommand selectCommand = conDataBase.CreateCommand();
                selectCommand.CommandText = GetCustomerDetail;
                selectCommand.Parameters.AddWithValue("@CntId", CntId);

                SqlDataReader myReader;
                myReader = selectCommand.ExecuteReader();

                while (myReader.Read())
                {
                    //produkt data
                    int sCntId = Convert.ToInt32(myReader["CntId"].ToString());
                    string sCntSurname = myReader["Surname"].ToString();
                    string sCntName = myReader["Name"].ToString();
                    int sCntAge = Convert.ToInt32(myReader["CntAge"]);

                    CtnDetail = new Contact
                    {
                        Id = sCntId,
                        FirstName = sCntName,
                        LastName = sCntSurname,
                        Age = sCntAge
                    };
                }
            }
            catch (Exception ex) //v primeru napake se izvede to
            {
                MessageBox.Show(ex.Message);
            }
        }
        return CtnDetail;
    }
}

您没有在更改客户属性的地方发布任何代码。这是因为我没有任何代码。:)我很难理解这个概念,因为这对我来说是全新的。我应该如何进一步进行?谢谢我用按钮更新来更改它。这就是你的想法吗?有人能帮我吗?这是我唯一缺少的东西。不清楚你还在试图解决什么问题。调用
contactSource.ResetBindings(true)时,您希望发生什么
   public class Contact : INotifyPropertyChanged
{
private int id;
private string firstName;
private string lastName;
private int age;

public Contact(int id, string firstName, string lastName, int age)
{
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}

public Contact()
{

}

protected virtual void OnPropretyChanged(string propertyName)
{
    var handler = this.PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public int Id { get { return this.id; } set { if (this.id != value) { this.id = value; this.OnPropretyChanged("Id"); } } }
public string FirstName { get { return this.firstName; } set { if (this.firstName != value) { this.firstName = value; this.OnPropretyChanged("FirstName"); } } }
public string LastName { get { return this.lastName; } set { if (this.lastName != value) { this.lastName = value; this.OnPropretyChanged("LastName"); } } }
public int Age { get { return this.age; } set { if (this.age != value) { this.age = value; this.OnPropretyChanged("Age"); } } }

public event PropertyChangedEventHandler PropertyChanged;