Windows Forms中的資料繫結(二)

來源:互聯網
上載者:User
window|資料 運行
我們運行這個程式來看看是否國家可以正常的顯示了。
1. 按下F5來運行程式。
2. 點擊Countries下拉框來看看是否國家資料已經可以顯示了。如果正常的話,你就可以看到如下圖8所示的程式:

圖8.使用ComboBox來顯示小資料集可以提高效能
帶參數的查詢來顯示資料
現在已經可以看到ComboBox中的國家資料了,接著我們就來做選擇ComboBox中的一個國家,在DataGrid中只顯示這個國家的客戶資料。我們按照這些步驟來做:
1. 把form的load事件中讀取DataGrid中資料的代碼刪掉。
2. 修改sqlDataAdapter1的SelectCommand屬性的SQL查詢語句,在查詢語句中增加我們要使用的參數。
3. 增加代碼來處理使用者選擇國家後的事件。
刪掉form的load事件中的讀取DataGrid資料的代碼
因為DataGrid中的資料顯示將由ComboBox中被選擇的國家來定,我們就不再需要form的load事件中使用的代碼。把form的load事件中的下列代碼刪掉:
DsCustomers1.Clear()
SqlDataAdapter1.Fill(DsCustomers1, "Customers")
修改SelectCommand屬性
接著我們就來修改sqlDataAdapter1的SelectCommand屬性,把要使用的參數加到這個查詢中去。按照下面的步驟操作:
1. 在form的設計視窗,點擊sqlDataAdapter1對象。
2. 按下F4來顯示內容視窗。
3. 點擊SelectCommand左邊的+號來展開SelectCommand的子屬性。
4. 點擊CommandText屬性,點擊Build(…)來顯示Query Builder對話方塊。
5. 在SQL查詢語句中增加下面的where語句:
SELECT CustomerID, CompanyName, ContactName, Country
FROM Customers
WHERE Country = @CountryParam
6. 點擊OK
7. 點擊sqlDataAdapter1對象
8. 點擊Data,點擊Generate DataSet,然後點擊OK來重建已有的DataSet。

圖9. 在SQL查詢語句中增加查詢參數
增加代碼來處理使用者選擇國家之後的事件
當使用者選擇了國家之後,我們來添加要改變DataGrid中資料的代碼。照下面的步驟作就可以實現這個功能:
1. 點擊cboCountry的SelectedIndexChanged事件
2. 設定DataGrid的查詢參數為cboCountry中被選擇的值
3. 填充DataSet中的資料
按照以下步驟來增加代碼:
1. 開啟form為設計視窗
2. 雙擊cboCountry來顯示SelectedIndexChanged事件的代碼。這個事件發生在使用者改變選擇的cboCountry值的時候。
3. 在這個事件中手寫下面的代碼:
Private Sub cboCountry_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cboCountry.SelectedIndexChanged
' Get the Parameter object and Set value
With SqlDataAdapter1.SelectCommand.Parameters
.Item("@CountryParam").Value = _
cboCountry.SelectedValue
End With
' Clear the dataset
DsCustomers1.Clear()
' Load the dataset using the parameter value
SqlDataAdapter1.Fill(DsCustomers1, "Customers")
End Sub
我們使用sqlDataAdapter1的SelectCommand屬性來取得Parameter對象,然後把這個對象的Value屬性設為cboCountry的SelectedValue屬性。由於我們設定了cboCountry的ValueMember屬性為Country欄位,因此cboCountry的SelectedValue屬性就為國家的值。設定好Value屬性後,就可以把資料填充到DataSet中了,DataGrid就自動顯示對應國家的客戶資料。
運行
現在可以看看我們的程式究竟是怎麼工作的了。
1. 按下F5來運行程式。
2. 在ComboBox中選擇一個國家,就可以在DataGrid中看到對應國家的客戶資料
3. 選擇不同的國家,來顯示不同國家的客戶資料
D. 使用TextBox的資料繫結
前面的例子都是使用DataGrid來顯示資料,在我們的程式中,使用TextBox來顯示單獨的行資料提供編輯同樣是非常重要的。這篇文章沒有將怎樣編輯資料,我們只講如何在TextBox中顯示資料。下面是主要的步驟:
1. 產生一個類似於圖10的Form
2. 產生、配置你要使用的DataSet
3. 添加控制項到form中,並且把他們綁定到資料來源上
4. 添加導覽按鈕,提供一行一行瀏覽資料的功能
按照上面的幾個步驟做下來,我們建立一個讀取Customers表的DataSet。在添加SqlDataAdapter的時候,我們選擇已有的串連到Northwind資料庫的串連,產生的SQL查詢語句中從Customers表中的CustomerID, CompanyName, ContactName, 和ContactTitle列。接著就可以添加控制項,並且綁定資料了。

圖10. 我們使用的簡單的例子視窗
添加控制項到form並且綁定資料
添加控制項到form中,並且設定他們的屬性為表1指定的值:
表1.用於form的控制項(如圖10所示)
Control Type Property Value
Label Name Label1
Text CustomerID
TextBox Name txtCustomerID
Text Blank
Label Name Label2
Text Company Name
TextBox Name txtCompanyName
Text Blank
Label Name Label3
Text Contact Name
TextBox Name txtContactName
Text Blank
Label Name Label4
Text Contact Title
TextBox Name txtContactTitle
Text Blank
CommandButton Name btnPrevious
Text <
CommandButton Name btnNext
Text >
接著就把每一個TextBox綁定到DataSet的一列上,我們按照下面的步驟作:
1. 選擇要綁定資料的TextBox
2. 按下F4來顯示內容視窗
3. 點擊展開DataBindings屬性
4. 在DataBindings屬性下,選擇Text屬性
5. 開啟下拉式清單把資料繫結到對應的列上。
比如:要綁定txtCustomerID到CustomerID列上,點擊dsCustomers1的+號,然後選擇CustomerID。
在所有的TextBox都綁定好之後,就像是在DataGrid中顯示資料那樣,我們同樣要在form的load事件中手寫代碼來匯出資料。下面就是我們添加的代碼:
Public Sub New()
MyBase.New()
' This call is required by the
' Windows Form Designer.
InitializeComponent()
' Add any initialization
' after the InitializeComponent() call
DsCustomers1.Clear()
SqlDataAdapter1.Fill(DsCustomers1, "Customers")
End Sub
添加按鈕來實現一行一行的導航
最後一步就是添加按鈕和代碼來允許使用者實現一行一行的導航。向前置航的按鈕代碼如下所示:
Private Sub btnPrevious_Click _
(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnPrevious.Click
Me.BindingContext(DsCustomers1, _
"Customers").Position -= 1
End Sub
程式中使用BindingContext來減少資料集的記錄指標。BindingContext跟蹤form上每一個資料來源的當前項。向後導航的按鈕代碼如下:
Private Sub btnNext_Click _
(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles btnNext.Click
Me.BindingContext(DsCustomers1, _
"Customers").Position += 1
End Sub
程式中使用BindingContext來增加資料集的記錄指標。我們的例子結果就如圖10所示的那樣。
注意:和DataGrid控制項例子一樣,在實際的應用中,我們同樣要減少顯示在form上的資料。比如,我們應該在form中添加一個ComboBox,並且允許使用者選擇指定國家的客戶資料。導覽按鈕的作用就只是在這個國家的客戶資料中實現。
Visual Basic 6.0中的不同點
Data binding with Windows Forms is far more robust than data binding in Visual Basic 6.0. With Visual Basic 6.0, you had little control over how the data was bound or what was going on underneath the covers. Using Visual Basic 6.0, when you used bound forms you added a data control and data entry controls to a form. You were basically stuck with the functionality that the data control provided. It was very difficult to troubleshoot or modify the behavior of the data control, because Microsoft did not reveal the source code behind the control to you.
Using data binding with Windows Forms and ADO.NET, you have much better control how the data is bound and how the form behaves. Data binding utilizes the ADO.NET classes and generates class code that you can view and modify. This means that when things don't work, or don't work the way that you intended them to, you are not left with your hands up in the air as you were with Visual Basic 6.0. Data binding with all its limitations in Visual Basic 6.0 was not the optimal choice for production applications; data binding with Windows Forms and ADO.NET is a viable option for the .NET developer.
/*
.NET中的資料繫結比Visual Basic 6.0中的資料繫結健壯多了。在Visual Basic 6.0中,我們能夠使用的資料繫結的控制項很少,背景程式同樣也支援不夠。在Visual Basic 6.0中,
*/
E. 總結
資料繫結讓我們在編寫程式的時候節省相當多的時間。使用資料繫結就不再需要寫像在Visual Basic 6.0要的所有的綁定資料的代碼了。在這篇文章裡,我們介紹了如何使用串連SQL Server資料庫的特定的對象,串連其他的資料庫同樣有相應的對象。一般的,這些都不需要手寫太多的代碼。
在這篇文章中,我們學到了:
1. 資料繫結的基本知識
2. 如何產生資料繫結的form
3. 如何和TextBox、ComboBox、DataGrid協作
4. 如何限制顯示在DataGrid中的資料
5. 如何建立資料導航的form
6. 如何?資料導航




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.