Username: Password:

添加一个下拉框到DataGrid-.NET教程,数据库应用
来源:作者: 发布时间:2007-12-25 13:34:37

本实例利用paint方法添加一个下拉框到datagrid1上 office:office" />
1、新建一个visual basic project 。
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
    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运行  

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅