在這里我們來講一篇關于如何進行數據列內容與顯示操作的方法。這種方法類似于實現:有數據表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) 的形式來構建要顯示的是“男”,“女”,而實際上存儲的是true和false呢?當然,如果我們使用datagridboolcolumn,通過設置它的一些屬性(truevalue,falsevalue)可以達到類似的效果,但對于那些非bit列呢?回答是肯定的。我們使用繼承datagridtextboxcolumn類,然后重寫getcolumnvalueatrow方法,來達到效果。getcolumnvalueatrow方法,把要從數據源的數據取出,然后判斷后,返回我們想要在網格中顯示的值。(代碼見后面的詳細代碼)
這樣,我們可以順利地在網格中顯示我們想要的數據了,但是還有另外一個問題,就是如果我們想在網格中修改數據,那么是否可以被提交給數據庫呢?如果僅僅通過上面的操作,只是達到了顯示的目的,還要重寫edit,commit,abort方法,來達到點擊單元格后修改內容,然后提交,最后更新到數據庫。
效果圖1
'************************************************************************************
'程序名稱:cansetvaluedatagridtextbox
'功能說明:繼承自datagridtextboxcolumn類的列樣式,主要實現顯示值與實際值的顯示與更新
'參數說明:無
'返回值 :cansetvaluedatagridtextbox
'編寫人員:閔峰
'日期時間:
'遺留問題:點擊列標題排序會發生顯示值的改變,這是一個bug嗎?有待解決
'************************************************************************************
public class cansetvaluedatagridtextbox
inherits system.windows.forms.datagridtextboxcolumn
#region " windows 窗體設計器生成的代碼 "
public sub new()
mybase.new()
'該調用是 windows 窗體設計器所必需的。
initializecomponent()
'在 initializecomponent() 調用之后添加任何初始化
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 窗體設計器所必需的
private components as system.componentmodel.icontainer
'注意:以下過程是 windows 窗體設計器所必需的
'可以使用 windows 窗體設計器修改此過程。
'不要使用代碼編輯器修改它。
<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 '存儲的真值
'-------------以下內容操作數據的顯示---------------
'重寫該過程是為了以合適的形式來顯示數據(★)
protected overrides function getcolumnvalueatrow(byval source as system.windows.forms.currencymanager, byval rownum as integer) as object
'從數據源獲得指定行的數據(注意:這里使用的類型是object)
dim result as object = mybase.getcolumnvalueatrow(source, rownum)
'顯示的設置
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
'---------------以下內容操作更新------------------
private isediting as boolean = false '是否在修改狀態
private oldvalue as object '原始值
'點擊單元格,準備編輯(★)
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") '---------
'直接觸發父類事件,是為了達到點擊后選中文本的效果。(如果不觸發就要自己把textbox移動到對應的邊框內)
mybase.edit(source, rownum, bounds, [readonly], instanttext, cellisvisible)
'設置修改碼
isediting = true
'記錄原始值
oldvalue = me.getcolumnvalueatrow(source, rownum)
end sub
'對輸入的修改進行提交(★)
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
'獲得單元格的內容
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
'顯示類型轉換函數
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("輸入非法,請檢查輸入后準備提交的值!")
end if
end function
end class
新聞熱點
疑難解答