2012-05-18 3 views
1

두 가지 형식이 있습니다. 기본 양식 및 하위 양식. 기본 폼에 표시 datagridview 및 하위 폼에있는 데이터 양식을 기본 폼에서 데이터를 삽입하는 .. .. 그래서 내가 자식 폼에서 데이터를 삽입 할 기본 폼에서 DataGridview 새로 고치려면. 새 데이터가 DataGridview에 나타납니다.다른 양식의 데이터 격자 새로 고침

public void button1_Click(object sender, EventArgs e) 
    { 
     string cstr = "server=localhost;User Id=root;database=sma9"; 
     con1 = new MySqlConnection(cstr); 
     con1.Open(); 
     com1 = new MySqlCommand(); 
     com1.Connection = con1; 
     com1.CommandType = CommandType.Text; 
     com1.CommandText = "INSERT INTO tbukux (kodebuku,judulbuku,namakategori,pengarang,penerbit,tahunterbit,stokbuku) VALUES ('" + txtkode.Text + "','" + txtjudul.Text + "','" + txtkategori.Text + "','" + txtpengarang.Text + "','" + txtpenerbit.Text + "','" + txttahun.Text + "','" + txtstok.Text + "')"; 
     com1.ExecuteNonQuery();    
     con1.Close(); 
     Form1 form1 = new Form1(); 
     form1.gridbuku.RefreshEdit();       
    } 

내가이 시도했지만 너무

public void button1_Click(object sender, EventArgs e) 
    { 
     Form1 form1 = new Form1(); 
     string cstr = "server=localhost;User Id=root;database=sma9"; 
     con1 = new MySqlConnection(cstr); 
     con1.Open(); 
     com1 = new MySqlCommand(); 
     com1.Connection = con1; 
     com1.CommandType = CommandType.Text; 
     com1.CommandText = "INSERT INTO tbukux (kodebuku,judulbuku,namakategori,pengarang,penerbit,tahunterbit,stokbuku) VALUES ('" + txtkode.Text + "','" + txtjudul.Text + "','" + txtkategori.Text + "','" + txtpengarang.Text + "','" + txtpenerbit.Text + "','" + txttahun.Text + "','" + txtstok.Text + "')"; 
     com1.ExecuteNonQuery(); 
     com2 = new MySqlCommand(); 
     com2.Connection = con1; 
     com2.CommandType = CommandType.Text; 
     com2.CommandText = "select * from tbukux"; 
     ds1 = new DataSet(); 
     adp1 = new MySqlDataAdapter(com2); 
     adp1.Fill(ds1, "tbukux"); 
     form1.gridbuku.DataSource = ds1; 
     form1.gridbuku.DataMember = "tbukux"; 
     con1.Close();    
     form1.gridbuku.Refresh();       
    } 
+0

try form1.gridbuku.DataSource = null 그리드를 새로 고침해야 할 때마다 데이터 소스를 다시 지정하십시오. – Sadaf

+0

@husnain, 작동하지 않음 선생님 : ( –

답변

1

작동하지 않습니다 ... 나는이 코드를하려고했으나 DataGridView를 내가 내 응용 프로그램을 닫고 새 DataGridView를 보여를 다시 열어야합니다, 새로 고칠 수 없습니다 당신은

public event System.Action NotifyAnotherForm 

는 이제 기본 폼에 btnOpenForm_click 아래에서이 이벤트를 넣어 frmNewBook ​​

이 같은 새 이벤트를 추가 할 수 있습니다

기본 폼에서
frmNewBook form = new frmNewBook(); 
form.NotifyAnotherForm += new System.Action(mainForm_NotifyAnotherForm); 
form.Show(); // or .ShowDialog(); 

, 당신은 당신이 frmNewBook에 약간의 수정 작업을 수행 할 때, 그것의 적절한 위치

if (NotifyAnotherForm != null) { 
    NotifyAnotherForm(); 
} 

을에서 이벤트를 할 것입니다이 이벤트를

public void mainForm_NotifyAnotherForm() { 
    //put you code for update datagrid 
} 

을 처리 할 수있는 방법이 있어야합니다 그리고 당신이 더 쉬운 방법이라면, 이것처럼해볼 수 있습니다.

frmNewBook form = new frmNewBook(); 

//You code will pause until form.Show close 
form.Show(); // or .ShowDialog(); 

//After frmNewBook close, code will continue running and you can put code to update 
loadDataGrid(); 

요 u 매개 변수을 SQL COMMAND에서 피하려면 Sql Injection

0

또한 "사용"을 사용할 수 있습니다.

 public void button1_Click(object sender, EventArgs e) 
    { 
     frmChildForm ofrmchild = new frmChildForm(); 

      using(new frmChildForm()) 

     { 
      ofrmchild .BringToFront(); 
      ofrmchild .ShowDialog(); 

     } 
     loadDataGrid(); 

    }