|
運(yùn)行 我們運(yùn)行這個(gè)程序來(lái)看看是否國(guó)家可以正常的顯示了。 1. 按下F5來(lái)運(yùn)行程序。 2. 點(diǎn)擊Countries下拉框來(lái)看看是否國(guó)家數(shù)據(jù)已經(jīng)可以顯示了。如果正常的話,你就可以看到如下圖8所示的程序:
圖8.使用ComboBox來(lái)顯示小數(shù)據(jù)集可以提高性能 帶參數(shù)的查詢來(lái)顯示數(shù)據(jù) 現(xiàn)在已經(jīng)可以看到ComboBox中的國(guó)家數(shù)據(jù)了,接著我們就來(lái)做選擇ComboBox中的一個(gè)國(guó)家,在DataGrid中只顯示這個(gè)國(guó)家的客戶資料。我們按照這些步驟來(lái)做: 1. 把form的load事件中讀取DataGrid中數(shù)據(jù)的代碼刪掉。 2. 修改sqlDataAdapter1的SelectCommand屬性的SQL查詢語(yǔ)句,在查詢語(yǔ)句中增加我們要使用的參數(shù)。 3. 增加代碼來(lái)處理用戶選擇國(guó)家后的事件。 刪掉form的load事件中的讀取DataGrid數(shù)據(jù)的代碼 因?yàn)镈ataGrid中的數(shù)據(jù)顯示將由ComboBox中被選擇的國(guó)家來(lái)定,我們就不再需要form的load事件中使用的代碼。把form的load事件中的下列代碼刪掉: DsCustomers1.Clear() SqlDataAdapter1.Fill(DsCustomers1, "Customers") 修改SelectCommand屬性 接著我們就來(lái)修改sqlDataAdapter1的SelectCommand屬性,把要使用的參數(shù)加到這個(gè)查詢中去。按照下面的步驟操作: 1. 在form的設(shè)計(jì)窗口,點(diǎn)擊sqlDataAdapter1對(duì)象。 2. 按下F4來(lái)顯示屬性窗口。 3. 點(diǎn)擊SelectCommand左邊的+號(hào)來(lái)展開SelectCommand的子屬性。 4. 點(diǎn)擊CommandText屬性,點(diǎn)擊Build(…)來(lái)顯示Query Builder對(duì)話框。 5. 在SQL查詢語(yǔ)句中增加下面的where語(yǔ)句: SELECT CustomerID, CompanyName, ContactName, Country FROM Customers WHERE Country = @CountryParam 6. 點(diǎn)擊OK 7. 點(diǎn)擊sqlDataAdapter1對(duì)象 8. 點(diǎn)擊Data,點(diǎn)擊Generate DataSet,然后點(diǎn)擊OK來(lái)重新生成已有的DataSet。
圖9. 在SQL查詢語(yǔ)句中增加查詢參數(shù) 增加代碼來(lái)處理用戶選擇國(guó)家之后的事件 當(dāng)用戶選擇了國(guó)家之后,我們來(lái)添加要改變DataGrid中數(shù)據(jù)的代碼。照下面的步驟作就可以實(shí)現(xiàn)這個(gè)功能: 1. 點(diǎn)擊cboCountry的SelectedIndexChanged事件 2. 設(shè)置DataGrid的查詢參數(shù)為cboCountry中被選擇的值 3. 填充DataSet中的數(shù)據(jù) 按照以下步驟來(lái)增加代碼: 1. 打開form為設(shè)計(jì)窗口 2. 雙擊cboCountry來(lái)顯示SelectedIndexChanged事件的代碼。這個(gè)事件發(fā)生在用戶改變選擇的cboCountry值的時(shí)候。 3. 在這個(gè)事件中手寫下面的代碼: 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屬性來(lái)取得Parameter對(duì)象,然后把這個(gè)對(duì)象的Value屬性設(shè)為cboCountry的SelectedValue屬性。由于我們?cè)O(shè)置了cboCountry的ValueMember屬性為Country字段,因此cboCountry的SelectedValue屬性就為國(guó)家的值。設(shè)置好Value屬性后,就可以把數(shù)據(jù)填充到DataSet中了,DataGrid就自動(dòng)顯示對(duì)應(yīng)國(guó)家的客戶資料。 運(yùn)行 現(xiàn)在可以看看我們的程序究竟是怎么工作的了。 1. 按下F5來(lái)運(yùn)行程序。 2. 在ComboBox中選擇一個(gè)國(guó)家,就可以在DataGrid中看到對(duì)應(yīng)國(guó)家的客戶資料 3. 選擇不同的國(guó)家,來(lái)顯示不同國(guó)家的客戶資料 D. 使用TextBox的數(shù)據(jù)綁定 前面的例子都是使用DataGrid來(lái)顯示數(shù)據(jù),在我們的程序中,使用TextBox來(lái)顯示單獨(dú)的行數(shù)據(jù)提供編輯同樣是非常重要的。這篇文章沒(méi)有將怎樣編輯數(shù)據(jù),我們只講如何在TextBox中顯示數(shù)據(jù)。下面是主要的步驟: 1. 生成一個(gè)類似于圖10的Form 2. 生成、配置你要使用的DataSet 3. 添加控件到form中,并且把他們綁定到數(shù)據(jù)源上 4. 添加導(dǎo)航按鈕,提供一行一行瀏覽數(shù)據(jù)的功能 按照上面的幾個(gè)步驟做下來(lái),我們創(chuàng)建一個(gè)讀取Customers表的DataSet。在添加SqlDataAdapter的時(shí)候,我們選擇已有的連接到Northwind數(shù)據(jù)庫(kù)的連接,生成的SQL查詢語(yǔ)句中從Customers表中的CustomerID, CompanyName, ContactName, 和ContactTitle列。接著就可以添加控件,并且綁定數(shù)據(jù)了。
圖10. 我們使用的簡(jiǎn)單的例子窗口 添加控件到form并且綁定數(shù)據(jù) 添加控件到form中,并且設(shè)置他們的屬性為表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 > 接著就把每一個(gè)TextBox綁定到DataSet的一列上,我們按照下面的步驟作: 1. 選擇要綁定數(shù)據(jù)的TextBox 2. 按下F4來(lái)顯示屬性窗口 3. 點(diǎn)擊展開DataBindings屬性 4. 在DataBindings屬性下,選擇Text屬性 5. 打開下拉列表把數(shù)據(jù)綁定到對(duì)應(yīng)的列上。 比如:要綁定txtCustomerID到CustomerID列上,點(diǎn)擊dsCustomers1的+號(hào),然后選擇CustomerID。 在所有的TextBox都綁定好之后,就像是在DataGrid中顯示數(shù)據(jù)那樣,我們同樣要在form的load事件中手寫代碼來(lái)導(dǎo)出數(shù)據(jù)。下面就是我們添加的代碼: 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 添加按鈕來(lái)實(shí)現(xiàn)一行一行的導(dǎo)航 最后一步就是添加按鈕和代碼來(lái)允許用戶實(shí)現(xiàn)一行一行的導(dǎo)航。向前導(dǎo)航的按鈕代碼如下所示: 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來(lái)減少數(shù)據(jù)集的記錄指針。BindingContext跟蹤form上每一個(gè)數(shù)據(jù)源的當(dāng)前項(xiàng)。向后導(dǎo)航的按鈕代碼如下: 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來(lái)增加數(shù)據(jù)集的記錄指針。我們的例子結(jié)果就如圖10所示的那樣。 注意:和DataGrid控件例子一樣,在實(shí)際的應(yīng)用中,我們同樣要減少顯示在form上的數(shù)據(jù)。比如,我們應(yīng)該在form中添加一個(gè)ComboBox,并且允許用戶選擇指定國(guó)家的客戶資料。導(dǎo)航按鈕的作用就只是在這個(gè)國(guó)家的客戶資料中實(shí)現(xiàn)。 Visual Basic 6.0中的不同點(diǎn) 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中的數(shù)據(jù)綁定比Visual Basic 6.0中的數(shù)據(jù)綁定健壯多了。在Visual Basic 6.0中,我們能夠使用的數(shù)據(jù)綁定的控件很少,后臺(tái)的程序同樣也支持不夠。在Visual Basic 6.0中, */ E. 總結(jié) 數(shù)據(jù)綁定讓我們?cè)诰帉懗绦虻臅r(shí)候節(jié)省相當(dāng)多的時(shí)間。使用數(shù)據(jù)綁定就不再需要寫像在Visual Basic 6.0要的所有的綁定數(shù)據(jù)的代碼了。在這篇文章里,我們介紹了如何使用連接SQL Server數(shù)據(jù)庫(kù)的特定的對(duì)象,連接其他的數(shù)據(jù)庫(kù)同樣有相應(yīng)的對(duì)象。一般的,這些都不需要手寫太多的代碼。 在這篇文章中,我們學(xué)到了: 1. 數(shù)據(jù)綁定的基本知識(shí) 2. 如何生成數(shù)據(jù)綁定的form 3. 如何和TextBox、ComboBox、DataGrid協(xié)作 4. 如何限制顯示在DataGrid中的數(shù)據(jù) 5. 如何創(chuàng)建數(shù)據(jù)導(dǎo)航的form 6. 如何實(shí)現(xiàn)數(shù)據(jù)導(dǎo)航
|