Microsoft KB Archive/323167

= HOW TO: Add a ComboBox Control to a Windows Form DataGrid Control =

PSS ID Number: 323167

Article Last Modified on 10/26/2002

-

The information in this article applies to:


 * Microsoft Visual Basic .NET (2002)

-



This article was previously published under Q323167



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data.SqlClient
 * System.Windows.Forms

IN THIS TASK

 * SUMMARY
 * ** Create the Sample



SUMMARY
This step-by-step article describes one method that you can use to add a ComboBox control to a DataGrid control.

There are many ways to add a ComboBox control to a DataGrid control. Most of these methods involve using the ColumnStyles collection of the DataGrid. However, this article uses a simpler method, which is to add the drop-down ComboBox control to the Control collection of the DataGrid.

back to the top

Create the Sample
 Follow these steps to create a new Visual Basic Windows Application project:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Project under Project Types, and then click Windows Application under Templates. By default, Form1 is added.  Drag a DataGrid control from the toolbox to Form1.  Add the following code to the top of the code window in the Declarations section of Form1.vb: Imports System.Data.SqlClient Imports System.Windows.Forms </li>  Add the following code after the &quot;Windows Form Designer generated code&quot; section of the code window: Public MyCombo As New ComboBox Dim con As New SqlConnection(&quot;server=myservername;uid=myid;pwd=mypassword;database=northwind&quot;) Dim daEmp As New SqlDataAdapter(&quot;Select * From Employees&quot;, 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 = &quot;MyCombo&quot; MyCombo.Visible = False MyCombo.Items.Clear MyCombo.Items.Add(&quot;Sales Representative&quot;) MyCombo.Items.Add(&quot;Inside Sales Coordinator&quot;) MyCombo.Items.Add(&quot;Vice President, Sales&quot;) MyCombo.Items.Add(&quot;Sales Manager&quot;) MyCombo.Items.Add(&quot;Flunky&quot;)

daEmp.Fill(ds, &quot;Employees&quot;)

'Set the RowHeight of the DataGrid to the height of the ComboBox. DataGrid1.PreferredRowHeight = MyCombo.Height DataGrid1.DataSource = ds

DataGrid1.DataMember = &quot;Employees&quot; '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) & &quot;&quot; = &quot;&quot; Then SendKeys.Send(&quot;*&quot;) 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) & &quot;&quot; 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 </li> Modify the connection string as necessary for your environment.</li> Press F5 to run the project. Click one of the fields in the Title column in the DataGrid. Notice that the ComboBox control is located in the DataGrid.</li> Expand the ComboBox. Notice that a list of titles is displayed.</li></ol>

back to the top

Additional query words: place ComboBox inside within DataGrid

Keywords: kbhowto kbHOWTOmaster kbSqlClient kbSystemData KB323167

Technology: kbAudDeveloper kbVBNET kbVBSearch

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© 2003 Microsoft Corporation. All rights reserved.