HOW TO:將 ComboBox 控制項添加到 Windows 表單 DataGrid 控制項
適用於
本文引用下面的 Microsoft .NET 架構類庫名稱空間:
- System.Data.SqlClient
- System.Windows.Forms
本任務的內容
概要
本分步指南介紹一種您可以用來將 ComboBox 控制項添加到 DataGrid 控制項的方法。
有很多方法可以將 ComboBox 控制項添加到 DataGrid 控制項。其中多數方法涉及到使用 DataGrid 的 ColumnStyles 集合。不過,本文將使用一種較為簡單的方法,將下拉 ComboBox 控制項添加到 DataGrid 的控制項集合。
back to the top
建立樣本
- 按照如下步驟建立一個新的 Visual Basic Windows 應用程式項目:
- 啟動 Microsoft Visual Studio .NET。
- 在檔案菜單上,指向建立,然後單擊項目。
- 在建立項目對話方塊中,單擊物件類型下的 Visual Basic 項目,然後單擊模板下的 Windows 應用程式。預設情況下將添加 Form1。
- 將一個 DataGrid 控制項從工具箱到拖到 Form1。
- 將下面的代碼添加到代碼視窗頂部的 Form1.vb 的“聲明”部分中:
Imports System.Data.SqlClientImports System.Windows.Forms
- 將下面的代碼添加到代碼視窗的“Windows 表單設計器產生代碼”部分之後:
Public MyCombo As New ComboBox() Dim con As New SqlConnection("server=myservername;uid=myid;
pwd=mypassword;database=northwind") Dim daEmp As New SqlDataAdapter("Select * From Employees", con) Public ds As New DataSet() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged 'Fill ComboBox list. MyCombo.Name = "MyCombo" MyCombo.Visible = False MyCombo.Items.Clear() MyCombo.Items.Add("Sales Representative") MyCombo.Items.Add("Inside Sales Coordinator") MyCombo.Items.Add("Vice President, Sales") MyCombo.Items.Add("Sales Manager") MyCombo.Items.Add("Flunky") daEmp.Fill(ds, "Employees") 'Set the RowHeight of the DataGrid to the height of the ComboBox. DataGrid1.PreferredRowHeight = MyCombo.Height DataGrid1.DataSource = ds DataGrid1.DataMember = "Employees" 'Add ComboBox to the Control collection of the DataGrid. DataGrid1.Controls.Add(MyCombo) End Sub Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As_
System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint If DataGrid1.CurrentCell.ColumnNumber = 3 Then MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width End If End Sub Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) If DataGrid1.CurrentCell.ColumnNumber = 3 Then MyCombo.Visible = False If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then SendKeys.Send("*") End If DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text End If End Sub Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged If DataGrid1.CurrentCell.ColumnNumber = 3 Then MyCombo.Visible = False MyCombo.Width = 0 MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & "" MyCombo.Visible = True Else MyCombo.Visible = False MyCombo.Width = 0 End If End Sub Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Scroll MyCombo.Visible = False MyCombo.Width = 0 End Sub Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click MyCombo.Visible = False MyCombo.Width = 0 End Sub
- 根據您的環境需要修改連接字串。
- 按 F5 鍵運行該項目。單擊 DataGrid 中“標題”列中的一個欄位。您會注意到 ComboBox 控制項位於 DataGrid 中。
- 展開 ComboBox。您會注意到顯示出了一個標題列表。