2013-07-05 8 views
0

작은 문제가 있습니다. form1에서 datagridview를 사용하여 SQL 명령을 사용하여 form2에서 데이터를 삽입하지만 INSERT 명령을 포함하는 버튼을 클릭 한 후 새로 삽입 된 값이 표시되지 않습니다. form1의 datagridview. 이 문제를 해결할 방법이 있습니까?form1의 데이터 새로 고침 form2의 삽입 후 자동 새로 고침

그래서 뭔가를 삽입 할 때마다 새로 고침 할 수 있도록 form1에서 새로 고침 버튼을 만들어야했습니다.

미리 감사드립니다. 개인 무효 btn_zaj_uloz_Click (개체 보낸 사람, EventArgs입니다 전자) 코드에 {

 SqlCommand prikaz = new SqlCommand 
      ("INSERT INTO zajezd(akce,name,zeme,hotel,h_adresa,odjdate,pridate,pocdnu,pocnoc,klimax)values(@zakce,@zname,@zzeme,@zhotel,@zh_adresa,@zodjdate,@zpridate,@zpocdnu,@zpocnoc,@zklimax)", spojeni); 


     prikaz.Parameters.AddWithValue("zakce", zakce.Text); 
     prikaz.Parameters.AddWithValue("zname", zname.Text); 
     prikaz.Parameters.AddWithValue("zzeme", zzeme.Text); 
     prikaz.Parameters.AddWithValue("zhotel", zhotel.Text); 
     prikaz.Parameters.AddWithValue("zh_adresa", zh_adresa.Text); 
     prikaz.Parameters.AddWithValue("zodjdate", zodjdate.Text); 
     prikaz.Parameters.AddWithValue("zpridate", zpridate.Text); 
     prikaz.Parameters.AddWithValue("zpocdnu", zpocdnu.Text); 
     prikaz.Parameters.AddWithValue("zpocnoc", zpocnoc.Text); 
     prikaz.Parameters.AddWithValue("zklimax", zklimax.Text); 

     spojeni.Open(); 
     prikaz.ExecuteNonQuery(); 
     System.Data.DataTable dt = new System.Data.DataTable(); 
     System.Data.SqlClient.SqlDataAdapter SDA = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM zajezd", spojeni); 
     SDA.Fill(dt); 


     spojeni.Close(); 

     this.Close(); 



    } 

답변

1

일부 thoughs :

이 Form2를의 버튼 클릭으로 삽입 코드입니다. 내가 누락 된 것이 아니라면 스 니펫은 삽입 (OK)을 수행 한 다음 모든 데이터를 다시 읽지 만 결과에는 아무런 영향을 미치지 않으므로 사실 Form1은 실제로 무언가가 일어난 것을 결코 깨닫지 못합니다.

당신이해야 할 일은 Form2가 삽입을 마쳤다 고 Form1에 알려주고 데이터를 되돌려 보내는 방법입니다. 표에는 행을 표시하는 데 필요한 일종의 데이터 소스가 있어야하며 Form2에서 새로 만든 데이터를 추가해야합니다. 내가 생각할 수있는 가장 좋은 방법은 Form2에서 레코드를 재생성하는 데 필요한 모든 데이터를 전달하는 Form1에서 이벤트를 만드는 것입니다.

먼저 이벤트 핸들러 데이터 생성 : 여분는 "zajezd SELECT * FROM"대체 당신의 코드에서

public event EventHandler<InsertCompleteEventArgs> InsertComplete; 

protected void OnInsertComplete(string zakce, string zname /*other data*/) 
{ 
    EventHandler<InsertCompleteEventArgs> handler = this.InsertComplete; 
    if(handler!=null) 
    { 
     handler(this,new InsertCompleteEventArgs(){zakce=zakce,zname=zname}); 
    } 
} 

대신에 이벤트를 발생 : 선언, 이벤트를 Form2를에서

public class InsertCompleteEventArgs : EventArgs 
{ 
    public string zakce {get;set;} 
    public string zname {get;set;} 
    /*other fields go here*/ 
} 

을 :

This.OnInsertComplete(zakce.Text,zname.Text); 

그래서, 그와 함께, Form2를 이벤트에 새로운 레코드를 삽입 할 때마다 인상, 그래서 anyo이됩니다 신경이 쓰이는 부분은 스스로 업데이트 할시기를 알려줄 수 있습니다. Form2를 시작할 때 Form1에서 해당 이벤트를 승인하면됩니다.

public Button_Click(object sender, EventArgs e) 
{ 
    /* This will be your existing code when you show Form2 */ 
    Form2 form=new Form2(); 
    form.Insertcomplete += this.Form2_InsertComplete; //this is where the notification is requested 
    form.Show(); 
} 

public Form2_InsertComplete(object sender, InsertCompleteEventArgs e) 
{ 
    /* From here you add the new record to the existing DataSource of the DataGridView using the properties of the "e" object you receive */ 
}