首页 > 开发 > 综合 > 正文

如何在DataGrid中添加ComboBox的方法

2024-07-21 02:24:18
字体:
来源:转载
供稿:网友


有两中方法:

1.修改 columnstyles 属性的值.
2.在datagrid里, 直接贴 combo 控件. 如:

public mycombo as new combobox()
    dim con as new
sqlconnection("server=myservername;uid=myid;pwd=mypassword;database=northwin
d")
    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

============================
更多资料请查msdn.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表