2013-03-27 79 views
0

我有下面的代碼,它想要做什麼是更新時,我從一個單獨的表單添加新的記錄到我的表,現在我得到所有的表數據,但如果我添加新的數據,它是在這裏不可見,但保存在表格中,我必須關閉該程序並再次運行,才能使保存的數據在datagridview中可見。我的問題是我如何更新datagridview,以便表格信息始終在那裏。 代碼中,我有如下:更新datagridview

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace project 
{ 
    public partial class frmViewBookings : Form 
    { 
     public frmViewBookings() 
     { 
      InitializeComponent(); 
     } 

     private void btnClose_Click(object sender, EventArgs e) 
     { 
      Form3 mainpage = new Form3(); 
      mainpage.Show(); 
      this.Close(); 
     } 

     private void frmViewBookings_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed. 
      this.bookingTableAdapter.Fill(this.usersDataSet1.Booking); 

     } 
    } 
} 
+1

你叫'.DataBind()'更新後? – DGibbs 2013-03-27 16:52:19

+0

「綁定看起來像」是什麼?你可以顯示你在哪裏聲明'usersDataSet1'看起來像是在這裏丟失了一些代碼 – MethodMan 2013-03-27 16:52:27

+0

嗯......我想我很困惑,@DJKRAZE-我只是簡單地從工具箱中添加了datagridview,然後簡單地選擇了數據源。這是我所做的 – bandaa 2013-03-27 16:57:38

回答

0

添加/編輯您在frmViewBookings現有的方法:

private void frmViewBookings_Load(object sender, EventArgs e) 
{ 
    // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed. 
    UpdateData(); 

} 

public void UpdateData() 
{ 
    this.bookingTableAdapter.Fill(this.usersDataSet1.Booking); 
} 

然後調用UpdateData()從您的其他形式的

在你的其他形式,加屬於第一種形式的財產:

public partial class frmBooking : Form 
{ 
    public frmViewBookings myBookingsFrm = null; 
} 

Th當您創建frmBooking形式的連接,這樣做:

frmBooking frmNewBookingFrm = new frmBooking(); 
frmNewBookingFrm.myBookingsFrm = this;//or whatever reference to the first frmViewBookings is 

所以你插入內frmBooking新記錄後,撥打:

myBookingsFrm.UpdateData(); 
+0

謝謝,我如何在我的frmBooking窗體中調用「updateData()」? – bandaa 2013-03-27 16:54:13

+0

我有兩種形式,1是上面的一個,另一個是frmBooking,我在那裏做實際的預訂....我想在frmBooking上做預訂,然後在datagridview(上面的表單)中更新數據數據是在frmBooking上預訂後存儲的,但不會在datagridview上顯示,除非我關閉程序並再次運行它。這是我需要幫助的。謝謝 – bandaa 2013-03-27 17:16:48

+0

這正是我提供的代碼所做的。如果您提供frmBooking的代碼,我可以爲您編輯它。 – Rob 2013-03-27 17:18:11