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

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

重寫datagridtextboxcolumn實(shí)現(xiàn)設(shè)置顯示與實(shí)際列值?。ɡ缧詣e的顯示)

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

    在這里我們來講一篇關(guān)于如何進(jìn)行數(shù)據(jù)列內(nèi)容與顯示操作的方法。這種方法類似于實(shí)現(xiàn):有數(shù)據(jù)表test(id int not null primary key ,name varchar(20) ,sex bit ), 那么是否有方法不通過直接使用sql語句,如:select id ,name ,sex =case sex ( when true then ‘男’ when false then ‘女’ else sex end) 的形式來構(gòu)建要顯示的是“男”,“女”,而實(shí)際上存儲的是true和false呢?當(dāng)然,如果我們使用datagridboolcolumn,通過設(shè)置它的一些屬性(truevalue,falsevalue)可以達(dá)到類似的效果,但對于那些非bit列呢?回答是肯定的。我們使用繼承datagridtextboxcolumn類,然后重寫getcolumnvalueatrow方法,來達(dá)到效果。getcolumnvalueatrow方法,把要從數(shù)據(jù)源的數(shù)據(jù)取出,然后判斷后,返回我們想要在網(wǎng)格中顯示的值。(代碼見后面的詳細(xì)代碼)

這樣,我們可以順利地在網(wǎng)格中顯示我們想要的數(shù)據(jù)了,但是還有另外一個(gè)問題,就是如果我們想在網(wǎng)格中修改數(shù)據(jù),那么是否可以被提交給數(shù)據(jù)庫呢?如果僅僅通過上面的操作,只是達(dá)到了顯示的目的,還要重寫edit,commit,abort方法,來達(dá)到點(diǎn)擊單元格后修改內(nèi)容,然后提交,最后更新到數(shù)據(jù)庫。

 效果圖1下面是程序的完整代碼,這里只是拋磚引玉,希望你可以根據(jù)需要來作出相應(yīng)的改進(jìn)。

'************************************************************************************

'程序名稱:cansetvaluedatagridtextbox

'功能說明:繼承自datagridtextboxcolumn類的列樣式,主要實(shí)現(xiàn)顯示值與實(shí)際值的顯示與更新                                                

'參數(shù)說明:無

'返回值  :cansetvaluedatagridtextbox

'編寫人員:閔峰

'日期時(shí)間:2005-06-16上午

'遺留問題:點(diǎn)擊列標(biāo)題排序會發(fā)生顯示值的改變,這是一個(gè)bug嗎?有待解決

'************************************************************************************

public class cansetvaluedatagridtextbox

    inherits system.windows.forms.datagridtextboxcolumn

 

#region " windows 窗體設(shè)計(jì)器生成的代碼 "

 

    public sub new()

        mybase.new()

 

        '該調(diào)用是 windows 窗體設(shè)計(jì)器所必需的。

        initializecomponent()

 

        '在 initializecomponent() 調(diào)用之后添加任何初始化

 

    end sub

 

    'usercontrol 重寫 dispose 以清理組件列表。

    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 窗體設(shè)計(jì)器所必需的

    private components as system.componentmodel.icontainer

 

    '注意:以下過程是 windows 窗體設(shè)計(jì)器所必需的

    '可以使用 windows 窗體設(shè)計(jì)器修改此過程。

    '不要使用代碼編輯器修改它。

    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

        components = new system.componentmodel.container()

    end sub

 

#end region

    private rnum as integer '列序號

    private rname as string = "" '列名稱

    private m_type as type '列的類型

 

    public sub new(byval source as datatable, byval rnum as integer, byval displaytrue as string, byval truevalue as string, _

    byval displayfalse as string, byval falsevalue as string)

        rnum = rnum

        m_displaytrue = displaytrue

        m_valuetrue = truevalue

        m_displayfalse = displayfalse

        m_valuefalse = falsevalue

        m_type = source.columns(rnum).datatype

    end sub

 

    public sub new(byval source as datatable, byval rname as string, byval displaytrue as string, byval truevalue as string, _

    byval displayfalse as string, byval falsevalue as string)

        rname = rname

        m_displaytrue = displaytrue

        m_valuetrue = truevalue

        m_displayfalse = displayfalse

        m_valuefalse = falsevalue

        m_type = source.columns(rname).gettype

    end sub

 

    private m_displaytrue as string '顯示的真值

    private m_displayfalse as string '顯示的假值

    private m_valuefalse as string '存儲的假值

    private m_valuetrue as string '存儲的真值

 

    '-------------以下內(nèi)容操作數(shù)據(jù)的顯示---------------

    '重寫該過程是為了以合適的形式來顯示數(shù)據(jù)(★)

    protected overrides function getcolumnvalueatrow(byval source as system.windows.forms.currencymanager, byval rownum as integer) as object

        '從數(shù)據(jù)源獲得指定行的數(shù)據(jù)(注意:這里使用的類型是object)

        dim result as object = mybase.getcolumnvalueatrow(source, rownum)

        '顯示的設(shè)置

        if result.tostring = m_valuetrue then

            return me.m_displaytrue

        elseif result.tostring = m_valuefalse then

            return me.m_displayfalse

        elseif result is dbnull.value then

            if me.nulltext is nothing then

                return dbnull.value

            else

                return me.nulltext

            end if

        else

            throw new exception("該列中存在沒有指定顯示的字符!")

        end if

    end function

    '---------------以下內(nèi)容操作更新------------------

    private isediting as boolean = false '是否在修改狀態(tài)

    private oldvalue as object '原始值

 

    '點(diǎn)擊單元格,準(zhǔn)備編輯(★)

    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)

        console.writeline("edit") '---------

        '直接觸發(fā)父類事件,是為了達(dá)到點(diǎn)擊后選中文本的效果。(如果不觸發(fā)就要自己把textbox移動(dòng)到對應(yīng)的邊框內(nèi))

        mybase.edit(source, rownum, bounds, [readonly], instanttext, cellisvisible)

        '設(shè)置修改碼

        isediting = true

        '記錄原始值

        oldvalue = me.getcolumnvalueatrow(source, rownum)

    end sub

 

    '對輸入的修改進(jìn)行提交(★)

    protected overrides function commit(byval datasource as system.windows.forms.currencymanager, byval rownum as integer) as boolean

        console.writeline("in commit") '---------

        '如果修改碼為真則意味著修改完畢

        if not isediting then return true

        '獲得單元格的內(nèi)容

        dim currentvalue as object = me.textbox.text

        console.writeline(currentvalue) '---------

        try

            currentvalue = setsuitablevalue(currentvalue)

            setcolumnvalueatrow(datasource, rownum, currentvalue)

        catch ex as exception

            return false

        end try

        isediting = false

        invalidate()

        console.writeline("out commit") '---------

 

        return true

 

    end function

 

    '放棄的處理

    protected overrides sub abort(byval rownum as integer)

        console.writeline("abort")

        isediting = false

        me.textbox.text = oldvalue

        me.invalidate()

    end sub

 

    '顯示類型轉(zhuǎn)換函數(shù)

    private function getchangetype(byval s as string) as object

        select case m_type.tostring

            case "system.integer"

                console.writeline("integer")

                return convert.toint32(s)

            case "system.boolean"

                console.writeline("boolean")

                return convert.toboolean(s)

            case "system.string"

                return s

            case "system.decimal"

                return convert.todecimal(s)

        end select

    end function

    private function setsuitablevalue(byval s as string) as object

        if s = me.nulltext then

            return dbnull.value

        elseif s = m_displaytrue then

            return getchangetype(m_valuetrue)

        elseif s = m_displayfalse then

            return getchangetype(m_valuefalse)

        else

            throw new exception("輸入非法,請檢查輸入后準(zhǔn)備提交的值!")

        end if

    end function

end class

 

  • 網(wǎng)站運(yùn)營seo文章大全
  • 提供全面的站長運(yùn)營經(jīng)驗(yàn)及seo技術(shù)!
  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 温泉县| 彭泽县| 泽普县| 江永县| 鹤岗市| 吴川市| 石楼县| 安泽县| 绥滨县| 敖汉旗| 平远县| 正镶白旗| 武宁县| 靖边县| 临清市| 老河口市| 资中县| 淮安市| 杂多县| 星子县| 新平| 潜山县| 射阳县| 大庆市| 吉水县| 贵港市| 丹棱县| 大方县| 曲靖市| 仁怀市| 富宁县| 寿宁县| 保德县| 宁乡县| 扶余县| 九龙坡区| 秦安县| 齐齐哈尔市| 西乌珠穆沁旗| 阿拉善左旗| 贵州省|