本实例利用paint方法添加一个下拉框到datagrid1上 office:office” />
1、新建一个visual basic project 。
2、添加一个datagrid control到窗体上。
3、加入以下代码
2、添加一个datagrid control到窗体上。
3、加入以下代码
imports system.data.sqlclient
public class form1
inherits system.windows.forms.form
#region ” windows 窗体设计器生成的代码 “
public sub new()
mybase.new()
该调用是 windows 窗体设计器所必需的。
initializecomponent()
在 initializecomponent() 调用之后添加任何初始化
end sub
窗体重写处置以清理组件列表。
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 窗体设计器所必需的
private components as system.componentmodel.icontainer
注意:以下过程是 windows 窗体设计器所必需的
可以使用 windows 窗体设计器修改此过程。
不要使用代码编辑器修改它。
friend withevents datagrid1 as system.windows.forms.datagrid
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
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 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 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 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
end class
4、修连接字符串dim con as new sqlconnection(“server=lihg;uid=sa;pwd=sa;database=northwind”),使能连接上数据库
5、f5运行
5、f5运行