使用BindingSource綁定數據庫


1.前言

在 C# 中我們常見的綁定數據庫的方式是通過 ADO.NET 來實現的,除此之外,還可以直接在 form 的設計界面實現對數據源的綁定,這就是使用 BindingSource 實現的方法,下面將介紹這種方法。

2.綁定數據源

在 form 設計界面,點擊 DataGridView 右上角的箭頭,出現以下界面:

在 Choose Data Source 對應的下拉框中選擇或者創建數據源,

綁定數據源后的結果如下:

此時 DataGridView 會自動完成 Column 的創建,項目將會自動為其創建相關的控件,如下圖所示:

對應的在 Form 的中將顯示以下代碼:

private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'testDataSet.DebtorData' table. You can move, or remove it, as needed.
    this.debtorDataTableAdapter.Fill(this.testDataSet.DebtorData); }

此時,就完成了通過 BindingSource 實現了數據源的綁定。

3.實現增刪改查

暫時沒找到。

4.TableAdapter Update

private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'testDataSet.T1' table. You can move, or remove it, as needed.
    this.t1TableAdapter.Fill(this.testDataSet.T1); } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { t1TableAdapter.Update(testDataSet.T1); testDataSet.T1.AcceptChanges(); }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM