將 ComboBox 控制項添加到 Windows 表單 DataGrid 控制項

來源:互聯網
上載者:User
HOW TO:將 ComboBox 控制項添加到 Windows 表單 DataGrid 控制項

適用於

本文引用下面的 Microsoft .NET 架構類庫名稱空間:

  • System.Data.SqlClient
  • System.Windows.Forms

本任務的內容
  • 概要
    • 建立樣本

概要

本分步指南介紹一種您可以用來將 ComboBox 控制項添加到 DataGrid 控制項的方法。

有很多方法可以將 ComboBox 控制項添加到 DataGrid 控制項。其中多數方法涉及到使用 DataGridColumnStyles 集合。不過,本文將使用一種較為簡單的方法,將下拉 ComboBox 控制項添加到 DataGrid控制項集合。

back to the top

建立樣本
  1. 按照如下步驟建立一個新的 Visual Basic Windows 應用程式項目:

    1. 啟動 Microsoft Visual Studio .NET。
    2. 檔案菜單上,指向建立,然後單擊項目
    3. 建立項目對話方塊中,單擊物件類型下的 Visual Basic 項目,然後單擊模板下的 Windows 應用程式。預設情況下將添加 Form1。
  2. 將一個 DataGrid 控制項從工具箱到拖到 Form1。
  3. 將下面的代碼添加到代碼視窗頂部的 Form1.vb 的“聲明”部分中:
    Imports System.Data.SqlClientImports System.Windows.Forms
  4. 將下面的代碼添加到代碼視窗的“Windows 表單設計器產生代碼”部分之後:
    Public MyCombo As New ComboBox()    Dim con As New SqlConnection("server=myservername;uid=myid;
  5. 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 _
  6. 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_
  7.  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
  8. 根據您的環境需要修改連接字串。
  9. 按 F5 鍵運行該項目。單擊 DataGrid 中“標題”列中的一個欄位。您會注意到 ComboBox 控制項位於 DataGrid 中。
  10. 展開 ComboBox。您會注意到顯示出了一個標題列表。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.