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

首頁 > 開發 > 綜合 > 正文

一個通用的DataGridTableStyle的做法

2024-07-21 02:23:09
字體:
來源:轉載
供稿:網友
一個通用的datagridtablestyle的做法



哈,這兩天都喜歡寫“通用”的東西。

這個類,可以實現自適應列寬、只讀、時分顯示、事件、任意位置加列、單擊單元格背景色設置等等,操作簡便。只是時間關系(明天要出去一趟),今天沒辦法完善。僅供參考,你可以加入別的東西。以下只列代碼了,不清楚的自己試用查資料就行了。



public class tablestyle



private m_datagridtablestyle as datagridtablestyle

private m_datagrid as datagrid

private m_datatable as datatable

'//添加事件處理,在此只考慮datagridtextboxcolumn雙擊事件

public delegate sub clickeventhandler(byval sender as object, byval e as system.eventargs)

public event gridtextboxdoubleclickevent as clickeventhandler

public sub gridtextbox_doubleclick(byval sender as object, byval e as system.eventargs)

raiseevent gridtextboxdoubleclickevent(sender, e)

end sub

'//設置datagrid

public property [datagrid]() as datagrid

get

return m_datagrid

end get

set(byval value as datagrid)

m_datagrid = value

end set

end property

'//返回模板

public readonly property [datagridtablestyle]() as datagridtablestyle

get

return m_datagridtablestyle

end get

end property

'//初始化

public sub initialize()

'//判斷mdatagrid數據源類型

'//如果綁定的是dataset或dataviewmanager或沒有綁定任何數據源,則退出,

if typeof m_datagrid.datasource is system.data.dataset orelse _

typeof m_datagrid.datasource is system.data.dataviewmanager orelse _

m_datagrid.datasource is nothing then exit sub



'//以下分別考慮兩種數據源,一是dataview,一是datatable

if typeof m_datagrid.datasource is system.data.dataview then

m_datatable = ctype(m_datagrid.datasource, dataview).table

else

m_datatable = ctype(m_datagrid.datasource, datatable)

end if



m_datagridtablestyle = new datagridtablestyle

m_datagridtablestyle.mappingname = m_datatable.tablename

'//加columnstyle

dim mdatacolumn as datacolumn

dim mcolumnstyle as datagridcolumnstyle

for each mdatacolumn in m_datatable.columns



select case mdatacolumn.datatype.name

case "boolean"

mcolumnstyle = new datagridboolcolumn

case else

mcolumnstyle = new datagridtextboxcolumn

addhandler ctype(mcolumnstyle, datagridtextboxcolumn).textbox.doubleclick, addressof gridtextbox_doubleclick

end select



'//綁定到datatable的column

with mcolumnstyle

.mappingname = mdatacolumn.columnname

.headertext = mdatacolumn.columnname

end with



'//加入到datagridtablestyle

m_datagridtablestyle.gridcolumnstyles.add(mcolumnstyle)

next



'//將datagridtablestyle綁定到datagrid

m_datagrid.tablestyles.clear()

m_datagrid.tablestyles.add(m_datagridtablestyle)

end sub

'//自適應寬度

public sub autoextend()

if m_datagridtablestyle is nothing then exit sub

'取各字段的最大字節數,包括字段名和值

dim mrow as datarow

dim mcolumn as datacolumn

for each mcolumn in m_datatable.columns

m_datagridtablestyle.gridcolumnstyles(mcolumn.columnname).width = getcolumnmaxwidth(0, mcolumn.columnname)

next



for each mrow in m_datatable.rows

for each mcolumn in m_datatable.columns

if not isdbnull(mrow(mcolumn.columnname)) then

m_datagridtablestyle.gridcolumnstyles(mcolumn.columnname).width = _

getcolumnmaxwidth(m_datagridtablestyle.gridcolumnstyles(mcolumn.columnname).width, mrow(mcolumn.columnname).tostring)

end if

next

next



'參照datagrid的graphics賦實際寬度

for each mcolumnstyle as datagridcolumnstyle in m_datagridtablestyle.gridcolumnstyles

mcolumnstyle.width = columnwidth(mcolumnstyle.width)

next

end sub



private function getcolumnmaxwidth(byval maxwidth as integer, byval mstring as string) as integer

dim mlength as integer

mlength = system.text.encoding.default.getbytes(mstring).length()

if maxwidth < mlength then

return mlength

else

return maxwidth

end if

end function

private function columnwidth(byval maxwidth as integer) as integer

dim mgraphics as graphics = m_datagrid.creategraphics

dim mcolwidth as single

mcolwidth = mgraphics.measurestring(new string(ctype("a", char), maxwidth), m_datagrid.font).width + 2

return ctype(mcolwidth, integer)

end function

'//在某列后添加一列

public sub addcolumn(byval poscolumnname as string, byval columnname as string)

if m_datagridtablestyle is nothing then exit sub

if not m_datatable.columns.contains(poscolumnname) then exit sub

if m_datatable.columns.contains(columnname) then exit sub

dim tmpstyle as new datagridtablestyle

for each mcolumnstyle as datagridcolumnstyle in m_datagridtablestyle.gridcolumnstyles

tmpstyle.gridcolumnstyles.add(mcolumnstyle)

if mcolumnstyle.headertext.equals(poscolumnname) then

dim tmptextcolumn as new datagridtextboxcolumn

m_datatable.columns.add(columnname)

tmptextcolumn.headertext = columnname

tmptextcolumn.mappingname = columnname

tmpstyle.gridcolumnstyles.add(tmptextcolumn)

end if

next

m_datagrid.tablestyles.clear()

tmpstyle.mappingname = m_datagridtablestyle.mappingname

m_datagridtablestyle = tmpstyle

m_datagrid.tablestyles.add(m_datagridtablestyle)

end sub

'//不顯示null

public writeonly property notshownull() as boolean

set(byval value as boolean)

for each mcolumnstyle as datagridcolumnstyle in m_datagridtablestyle.gridcolumnstyles

if value then

mcolumnstyle.nulltext = ""

else

mcolumnstyle.nulltext = "(null)"

end if

next

end set

end property

'//如果是日期類型,顯示時間

public writeonly property showtimeformat() as boolean

set(byval value as boolean)

for each mcolumnstyle as datagridcolumnstyle in m_datagridtablestyle.gridcolumnstyles

if not mcolumnstyle.mappingname = "" andalso m_datatable.columns(mcolumnstyle.mappingname).datatype.name.indexof("date") <> -1 then

if value then

ctype(mcolumnstyle, datagridtextboxcolumn).format = "yyyy-mm-dd hh:mm:ss"

else

ctype(mcolumnstyle, datagridtextboxcolumn).format = "yyyy-mm-dd"

end if

end if

next

end set

end property

'個別編輯,除邏輯類型外

public readonly property textcolumnstyle(byval columnname as string) as datagridtextboxcolumn

get

if not m_datatable.columns(columnname) is nothing andalso not m_datatable.columns(columnname).datatype.name.equals("boolean") then

return ctype(m_datagridtablestyle.gridcolumnstyles(columnname), datagridtextboxcolumn)

else

return nothing

end if

end get

end property

end class



'測試

dim mytablestyle as new tablestyle

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

dim ds as new dataset

me.sqlconnection1.open()

me.sqldataadapter1.fill(ds)

me.sqlconnection1.close()

me.datagrid1.datasource = ds.tables(0)
addhandler mytablestyle.gridtextboxdoubleclickevent, addressof


end sub

private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click

with mytablestyle

.datagrid = me.datagrid1

.initialize()

end with

me.datagridtextcolumn_doubleclick

end sub

private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click

mytablestyle.notshownull = true

mytablestyle.showtimeformat = true

end sub



private sub button4_click(byval sender as system.object, byval e as system.eventargs) handles button4.click

mytablestyle.notshownull = false

mytablestyle.showtimeformat = false

end sub

private sub button5_click(byval sender as system.object, byval e as system.eventargs) handles button5.click

mytablestyle.datagridtablestyle.gridcolumnstyles(2).readonly = true

end sub



private sub button6_click(byval sender as system.object, byval e as system.eventargs) handles button6.click

mytablestyle.addcolumn("姓名", "hello")

mytablestyle.autoextend()

end sub



private sub button7_click(byval sender as system.object, byval e as system.eventargs) handles button7.click

mytablestyle.textcolumnstyle("姓名").width=0

end sub



private sub datagridtextcolumn_doubleclick(byval sender as object, byval e as system.eventargs)

dim mtextbox as textbox = ctype(sender, textbox)

mtextbox.backcolor = system.drawing.color.blue

msgbox(mtextbox.text)

end sub




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰和县| 八宿县| 凌海市| 高州市| 同心县| 皋兰县| 紫云| 鲜城| 高邮市| 宁德市| 绍兴县| 津市市| 剑阁县| 广宁县| 新闻| 安乡县| 临安市| 大洼县| 鲁山县| 榆社县| 岳阳市| 嵊泗县| 察雅县| 和政县| 荔浦县| 鄂托克前旗| 兴义市| 屏东县| 山西省| 长兴县| 平乡县| 彭阳县| 定结县| 平凉市| 六枝特区| 邻水| 伊金霍洛旗| 嘉禾县| 新宾| 黄冈市| 子长县|