人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

Windows Forms中的數據綁定(二)

Windows Forms中的數據綁定(二)

更新時間:2022-06-27 文章作者:未知 信息來源:網絡 閱讀次數:

運行
我們運行這個程序來看看是否國家可以正常的顯示了。
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. 如何實現數據導航


溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 塔河县| 曲沃县| 通海县| 巴塘县| 吴川市| 绥棱县| 吐鲁番市| 萨迦县| 任丘市| 保山市| 栾城县| 铜梁县| 汉沽区| 商都县| 浮山县| 靖州| 海原县| 上杭县| 仁怀市| 浏阳市| 平昌县| 大兴区| 通河县| 扎鲁特旗| 永宁县| 枞阳县| 麻阳| 资阳市| 浙江省| 敖汉旗| 石家庄市| 黑河市| 新平| 贡觉县| 德令哈市| 邵阳市| 广丰县| 南澳县| 洛南县| 卢龙县| 鄂托克前旗|