首页 > 开发 > 综合 > 正文

通用样式化Datagird

2024-07-21 02:22:50
字体:
来源:转载
供稿:网友
很多初学vb.net的新手,差不多都是对许多小功能不能实现而苦恼,
以下我个人在论坛中遇到的许多问题的综合的通用代码,供初学者研究学习:

'通用样式化datagird
'power by:landlordh
'调用:
' strheaders() 里面改为自已需要的列名
' iwidths() 里面改为相对应的列宽
' inputnum 在样式化之前赋值
' style(datagrid1, dataset1.tables("tabname"))
'
'=================================================================

'列名,字段对应名
public strheaders() as string = {"编号", "时间", "主题", "内容"}
'列宽,为0隐藏(优先级低于inputnum),对应的列宽
public iwidths() as integer = {0, 70, 130, 0}
'显示几列,为空则显示所有,为0则隐藏所有
public inputnum as string

public sub style(byval datagridname as datagrid, byval dataname as datatable)
try
datagridname.datasource = dataname
'显示新增
'dataname.defaultview.allownew = false
'在datagrid中直接编辑
'dataname.defaultview.allowedit = false
dim i as integer
'处理显示前几列的参数
if trim(inputnum) <> "" then
dim num as integer
try
num = cint(trim(inputnum))
try
for i = 0 to iwidths.length - 1
if i >= inputnum then
iwidths(i) = 0
end if
next
catch ex as exception
end try
catch ex as exception
msgbox("你输入的字符不符合要求")
end try
end if
dim ts as new datagridtablestyle
ts.mappingname = dataname.tablename
ts.alternatingbackcolor = system.drawing.color.papayawhip
ts.backcolor = system.drawing.color.whitesmoke
ts.selectionbackcolor = system.drawing.color.lightsteelblue
ts.selectionforecolor = color.black
ts.rowheaderwidth = 10
ts.preferredrowheight = 20
for i = 0 to dataname.columns.count - 1
dim ac as new datagridtextboxcolumn
'处理点击行
'addhandler ac.textbox.enter, addressof textboxenterhandler
ac.headertext = strheaders(i)
ac.mappingname = dataname.columns(i).columnname
ac.textbox.width = iwidths(i)
ts.preferredcolumnwidth = ac.textbox.width
'哪列,i=第几列数-1
if i = 1 then
ac.format = "yyyy-mm-dd hh:mm:ss"
'ac.readonly = true
'ac.nulltext = ""
ac.alignment = horizontalalignment.center
end if
if i = 1 or i = 2 then
'ac.readonly = true
'ac.nulltext = ""
ac.alignment = horizontalalignment.center
end if
ts.gridcolumnstyles.add(ac)
next
datagridname.tablestyles.clear()
datagridname.tablestyles.add(ts)
catch ex as exception
messagebox.show("格式化网格出错了")
end try
end sub

结束

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表