国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

向DataGrid控件中添加ComboBox控件

2024-07-21 02:24:17
字體:
供稿:網(wǎng)友

商業(yè)源碼熱門下載www.html.org.cn

 

在前面看到了很多關(guān)于怎樣向datagrid中添加combobox控件的方法。使用的方法全部都是在vb6.0中的方法。

我還是要說說在csnd中發(fā)貼的朋友。

現(xiàn)在所謂的.net編程人員,不知道是怎么了呢!只是停留在使用.net的編程環(huán)境中。并沒有真正的了解面向?qū)ο蟮?net編程思想。

我現(xiàn)在就利用繼承datagridcolumnstyle完成向datagrid中添加combobox。

希望這樣有助于大家了解真正的面向?qū)ο缶幊痰乃枷搿2灰皇钦J(rèn)為利用vb6.0中的某些方法就是.net高手了。有這種思想的人都是菜鳥 (希望這么說沒有得罪太多的朋友:)下面就是實現(xiàn)的代碼:我使用的是vb.net來完成的。

我熟悉c#,但是vb.net只是大概了解一下。應(yīng)該比一些人要高一點點吧!:)

見笑了!由于時間關(guān)系沒有協(xié)注釋,請見諒!

public class datagridcombocolumn
    inherits datagridcolumnstyle

    public withevents dgcombo as combobox = new combobox
    private isediting as boolean
    private _strselectedtext as string

    public sub new()
        mybase.new()
        dgcombo.visible = false
    end sub

    protected overrides sub abort(byval rownum as integer)
        isediting = false
        removehandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
        invalidate()

    end sub

    protected overrides function commit(byval datasource as system.windows.forms.currencymanager, byval rownum as integer) as boolean
        dgcombo.bounds = rectangle.empty
        addhandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
        if isediting = false then
            return true
        end if

        isediting = false
        try
            dgcombo.text = dgcombo.text
        catch ex as exception
            dgcombo.text = string.empty
        end try

        try
            dim value as string = _strselectedtext
            setcolumnvalueatrow(datasource, rownum, value)
        catch ex as exception
            abort(rownum)
            return false
        end try
        invalidate()
        return true

    end function

    protected overloads overrides sub edit(byval source as system.windows.forms.currencymanager, byval rownum as integer, byval bounds as system.drawing.rectangle, byval [readonly] as boolean, byval instanttext as string, byval cellisvisible as boolean)
        dim value as string
        try
            value = ctype(getcolumnvalueatrow(source, rownum), string)
        catch ex as exception
            setcolumnvalueatrow(source, rownum, dgcombo.text)
        end try

        value = ctype(getcolumnvalueatrow(source, rownum), string)

        if (cellisvisible) then
            dgcombo.bounds = new rectangle(bounds.x, bounds.y, bounds.width, bounds.height)
            dgcombo.text = value
            dgcombo.visible = true
            addhandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
        else
            dgcombo.text = value
            dgcombo.visible = false
        end if

        if dgcombo.visible = false then
            datagridtablestyle.datagrid.invalidate(bounds)
        end if
    end sub

    protected overrides function getminimumheight() as integer

    end function

    protected overrides function getpreferredheight(byval g as system.drawing.graphics, byval value as object) as integer

    end function

    protected overrides function getpreferredsize(byval g as system.drawing.graphics, byval value as object) as system.drawing.size

    end function

    protected overloads overrides sub paint(byval g as system.drawing.graphics, byval bounds as system.drawing.rectangle, byval source as system.windows.forms.currencymanager, byval rownum as integer)
        paint(g, bounds, source, rownum, true)
    end sub

    protected overloads overrides sub paint(byval g as system.drawing.graphics, byval bounds as system.drawing.rectangle, byval source as system.windows.forms.currencymanager, byval rownum as integer, byval aligntoright as boolean)
        paint(g, bounds, source, rownum, brushes.red, brushes.blue, aligntoright)
    end sub

    protected overloads overrides sub paint(byval g as system.drawing.graphics, _
                              byval bounds as system.drawing.rectangle, _
                              byval source as system.windows.forms.currencymanager, _
                              byval rownum as integer, _
                              byval backbrush as system.drawing.brush, _
                              byval forebrush as system.drawing.brush, _
                              byval aligntoright as boolean)

        dim strdate as string
        dim rect as rectanglef
        try
            strdate = dgcombo.text
            strdate = ctype(getcolumnvalueatrow(source, rownum), string)
        catch ex as exception
            setcolumnvalueatrow(source, rownum, dgcombo.text)
            strdate = ctype(getcolumnvalueatrow(source, rownum), string)
        end try

        rect.x = bounds.x
        rect.y = bounds.y
        rect.height = bounds.height
        rect.width = bounds.width

        g.fillrectangle(backbrush, rect)
        rect.offset(0, 2)
        rect.height -= 2

        g.drawstring(strdate, me.datagridtablestyle.datagrid.font, forebrush, rect)

    end sub

    protected overrides sub setdatagridincolumn(byval value as datagrid)
        mybase.setdatagridincolumn(value)
        if not (dgcombo.parent is nothing) then
            dgcombo.parent.controls.remove(dgcombo)
        end if
        if not (value is nothing) then
            value.controls.add(dgcombo)
        end if
    end sub

    private sub dgcombo_selectedvaluechanged(byval sender as object, byval e as system.eventargs) handles dgcombo.selectedvaluechanged
        isediting = true
        mybase.columnstartedediting(dgcombo)
        _strselectedtext = dgcombo.text
        if _strselectedtext is nothing then
            _strselectedtext = string.empty
        end if
    end sub
end class

以下是使用方法!

    private sub frmdatagrid_load(byval sender as object, byval e as system.eventargs) handles mybase.load
        call test()
    end sub

    private sub test()
        dim dt as new datatable("test")
        dt.columns.add(new datacolumn("id"))
        dt.columns.add(new datacolumn("combo"))
        call addgridstyle()
        call adddata(dt)
        dbgrid.datasource = dt

    end sub

    private sub addgridstyle()

        dim dbgridstyle as datagridtablestyle
        dim idcolumn as datagridtextboxcolumn
        dim dccolumn as datagridcombocolumn

        dbgridstyle = new datagridtablestyle
        dbgridstyle.mappingname = "test"

        idcolumn = new datagridtextboxcolumn
        idcolumn.mappingname = "id"
        idcolumn.headertext = "id"
        idcolumn.alignment = horizontalalignment.center
        idcolumn.width = 50
        dbgridstyle.gridcolumnstyles.add(idcolumn)

       dccolumn = new datagridcombocolumn
        dim i as integer
        for i = 0 to 25
            dccolumn.dgcombo.items.add((i + 1).tostring("00000"))
        next
        dccolumn.dgcombo.dropdownwidth = 120
        dccolumn.mappingname = "combo"
        dccolumn.headertext = "combo"
        dccolumn.alignment = horizontalalignment.center
        dccolumn.width = 60
        dbgridstyle.gridcolumnstyles.add(dccolumn)

       dbgrid.tablestyles.add(dbgridstyle)

    end sub

    private sub adddata(byref dt as datatable)
        dim drow as datarow
        dim introw as integer

        for introw = 0 to 9
            drow = dt.newrow()
            drow.item("id") = format(introw + 1, "000")
            drow.item("combo") = format(introw + 1, "00000")
            dt.rows.add(drow)
        next

        dt.acceptchanges()

    end sub

那么隨時在datagrid中所指定為combo列中單擊。combo就出現(xiàn)了

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙田区| 永丰县| 恩平市| 都匀市| 洪江市| 河东区| 马山县| 尼勒克县| 东阳市| 铜梁县| SHOW| 瑞昌市| 清河县| 青海省| 博兴县| 巨鹿县| 中方县| 威信县| 东明县| 扶绥县| 大港区| 唐海县| 长春市| 盖州市| 丹东市| 凤翔县| 美姑县| 会同县| 榆社县| 温泉县| 阳东县| 安吉县| 昌黎县| 金寨县| 科尔| 峨山| 噶尔县| 木里| 青河县| 徐闻县| 五常市|