How to: Add the ComboBox control to the Windows Form DataGrid control
Suitable
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.Data.sqlclient System.windows.Forms
This task content
summary
Create an example
Summary This step-by-step guide introduces a kind of you can use
ComboBox control added to
Method of DataGrid control.
There are many ways to
ComboBox control added to
DataGrid control. Most methods involved in use
DataGrid
ColumnStyles collection. However, this article will use a simpler way, pull down
ComboBox control added to
DataGrid
Control collection.
Back to the top
Create an example
Create a new Visual Basic Windows application item as follows:
Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click the Visual Basic project under the type of object, and then click Windows Applications under Templates. FORM1 will be added by default. Put a DataGrid control from the toolbox to Form1. Add the following code to the "Declaration" section of the FORM1.VB at the top of the code window: Imports System.Data.sqlClient
Imports System.Windows.Forms
Add the following code to the "Windows Form Designer Generator Generation Code" section of the code window: public myCombo as new comboBOBOX ()
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 E 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 Repesentative")
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 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 E 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 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 e as system.eventargs) Handles DataGrid1.Scroll
Mycombo.visible = false
Mycombo.width = 0
End Sub
Private sub DataGrid1_click (Byval e as system.eventargs) Handles DataGrid1.click
Mycombo.visible = false
Mycombo.width = 0
End Sub
Modify the connection string according to your environment. Press F5 to run the project. Click a field in the Title column in DataGrid. You will notice that the ComboBox control is in the DataGrid. Expand ComboBox. You will notice a list of headlines.
Back to the top
The information in this article applies to:
Microsoft Visual Basic .NET (2002)
Recent Update: 2003-6-9 (1.0) Keyword Kbhowto KbhowTomaster Kbsqlclient KbsystemData KB323167 KBAUDDEVEVELOPER