Add a drop down box to DataGrid

zhaozj2021-02-17  42

This example uses the Paint method to add a drop-down box to DataGrid1

1. Create a new Visual Basic Project. 2, add a DataGrid Control to the form. 3, add the following code

Imports system.data.sqlclient

Public Class Form1

Inherits System.Windows.Forms.form

#Region "Windows Form Designer Generated Code"

Public Sub New ()

Mybase.new ()

'This call is required for the Windows Form Designer.

InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

'Form rewriting disposal to clean the component list.

Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)

IF Disposing then

IF not (Components Is Nothing) THEN

Components.dispose ()

END IF

END IF

Mybase.dispose (Disposing)

End Sub

'Windows Form Designer

Private Components as System.comPonentModel.icontainer

'Note: The following process is necessary for the Windows Form Designer.

'You can modify this process using the Windows Form Designer.

'Don't modify it using the code editor.

Friend Withevents DataGrid1 As System.Windows.Forms.DataGrid

private subinitializecomponent ()

Me.DataGrid1 = new system.windows.Forms.DataGrid ()

Ctype (me.datagrid1, system.componentmodel.isupportinitialize) .beginInit ()

Me.suspendlayout ()

'

'DataGrid1

'

Me.DataGrid1.datamember = ""

Me.DataGrid1.HeaderforeColor = system.drawing.systemcolors.ControlText

Me.DataGrid1.location = new system.drawing.point (72, 56)

Me.DataGrid1.name = "DataGrid1"

Me.DataGrid1.size = new system.drawing.size (416, 184)

Me.DataGrid1.tabindex = 0

'

'Form1

'

Me.autoscalebasesize = new system.drawing.size (6, 14)

Me.ClientSize = new system.drawing.size (552, 285)

Me.Controls.addrange (new system.windows.forms.control () {me.datagrid1})

Me.Name = "Form1"

Me.Text = "Form1"

Ctype (me.datagrid1, system.componentmodel.isupportinitialize) .endinit ()

Me.ResumeLayout (false) End Sub

#End region

Public myCombo as new comboBox ()

DIM Con As New SqlConnection ("Server = LiHG; UID = SA; PWD = SA; 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

END CLASS

4, repair string DIM Con As new SqlConnection ("server = lihg; uid = sa; pwd = sa; database = northwind"), enable connection to database 5, F5 operation

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

New Post(0)