2017-04-24 1 views
0

업데이트 버튼을 누른 후 WinGridView를 업데이트하는 데 문제가 있습니다. INotifyPropertyChanged 속성을 사용하여 BindingList를 사용하고 있습니다. 그리고 여전히 내 gridview를 업데이트 할 수 없습니다. 다음은BindingList로 wingridview 업데이트

내 짧은 테스트 프로그램에서 코드입니다 :

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

     }   
    } 

사람은 throught를 가서 내가 잘못하고있는 무슨 얘기시겠습니까? 검색 및 stackoverflow 많은 질문을 테스트 한 여전히 올바르게 작동하도록 내 코드를 만들 수 없습니다.

+1

귀하는 고객의 자산을 변경 한 코드를 게시하지 않았습니다. – LarsTech

+0

그게 내가 없기 때문입니다. :)이 개념은 완전히 새로운 것이기 때문에 나는이 개념을 이해하는데 어려움을 겪고있다. 어떻게 더 진행해야합니까? 감사합니다. – Urban

+0

버튼 업데이트로 변경합니다. 그게 당신이 염두에두고있는 것입니까? – Urban

답변

0

그래서 나는 몇 가지 물건을 시험해 왔습니다. 내가 undestand 제대로 삽입/삭제 또는 업데이 트를 만들 때 나는 SQL 데이터 및 bindinglist의 변경 사항을 변경해야합니다.

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_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 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와 함께 작동합니다.