2013-03-27 45 views
0

我在我的winforms中有一個頁面,我想要在桌面上顯示所有數據,以便用戶可以在表格中看到信息,但在winforms上。我之前沒有這樣做過,所以需要你們的一些指導,我希望它能夠一致地(當添加新記錄時)更新並且是可編輯的,以便在需要時可以刪除一行。我的桌子是「預訂」,我可以在這張桌子上添加數據的形式是「frmBooking」,所以我希望在程序運行時將預訂數據存儲在datagridview中,以便用戶可以查看他們的預訂。感謝代碼,我是:如何在winforms中更新數據網格視圖?

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

     } 
    } 
} 
+2

你看過了綁定數據庫與數據庫? – 2013-03-27 15:41:30

+0

我用數據庫表綁定了數據網格,但只有現有的記錄纔會顯示,而不是在程序運行時產生的新記錄.... – bandaa 2013-03-27 15:43:49

+0

您是否添加了DGV控件來保存,添加記錄等?或者你是用你自己的代碼來做這件事嗎? – 2013-03-27 15:44:56

回答

2

可以使用結合在DataGridView到源在DataGridView的DataSource屬性。例如。一個DataTable。

無論何時編輯datatable,datagridview都會更新。

您也可以通過添加行和列來手動修改datagridview,但這有點多。

編輯:如果您希望將更改更新到datagridview,則需要修改作爲綁定源的數據表。

+0

謝謝,這樣做會更有意義,我會對它有所瞭解,看看它是如何發展的。 :) – bandaa 2013-03-27 15:51:33

+0

任何運氣。標記爲答案,如果它幫助你。 – CathalMF 2013-04-17 15:50:03