使用BindingList更新wingridview

时间:2017-04-24 17:54:46

标签: c# inotifypropertychanged bindingsource bindinglist

我在按下更新按钮后更新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问题,但仍无法使我的代码正常工作。

1 个答案:

答案 0 :(得分:0)

所以我一直在尝试一些东西。如果我在进行插入/删除或更新时没有正确检查,我必须更改SQL数据并更改绑定列表。

到目前为止我有这个

更新按钮:

    private void btn_UpdateCustomer_Click(object sender, EventArgs e)
    {
        UltraGridRow row = ugv_contacts.ActiveRow;
        Customers cust = customerList[row.Index];

        cust.Name = txt_name.Text;
        cust.Surname = txt_surname.Text;


        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();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

添加按钮(我找不到另一个重新绑定整个datagridview的解决方案):

    private void ultraButton1_Click(object sender, EventArgs e)
    {
        using (SqlConnection conDataBase = new SqlConnection(SQLconnectionString))
        {
            try
            {
                conDataBase.Open();
                SqlCommand insertCommand = conDataBase.CreateCommand();
                insertCommand.CommandText = InsertCustomerDet;

                insertCommand.Parameters.AddWithValue("@CntSurname", txt_surname.Text);
                insertCommand.Parameters.AddWithValue("@CntName", txt_name.Text);
                insertCommand.Parameters.AddWithValue("@CntStatus", true);

                SqlDataReader myReader;
                myReader = insertCommand.ExecuteReader();

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

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

删除按钮:

    private void btn_delete_Click(object sender, EventArgs e)
    {
        UltraGridRow row = ugv_contacts.ActiveRow;
        Customers cust = customerList[row.Index];

        if (cust != null)
        {
            customerList.Remove(cust);             
        }

        using (SqlConnection conDataBase = new SqlConnection(SQLconnectionString))
        {
            try
            {
                conDataBase.Open();
                SqlCommand deleteCommand = conDataBase.CreateCommand();
                deleteCommand.CommandText = DeleteCustomerDet;

                deleteCommand.Parameters.AddWithValue("@CntId", Customer_Id);

                SqlDataReader myReader;
                myReader = deleteCommand.ExecuteReader();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

是否有更好的解决方案来添加和删除绑定列表?更新适用于函数 - INotifyPropertyChanged。