Add a ComboBox control to a Windows Form DataGrid control

xiaoxiao2021-03-06  38

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 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 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.

转载请注明原文地址:https://www.9cbs.com/read-118526.html

New Post(0)