首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 认证考试-> Adobe认证
一个通用的DataGridTableStyle的做法-.NET教程,数据库应用
作者:网友供稿 点击:220
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
一个通用的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



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java开发工具配置 UltraEdit-JSP教程,Java技巧及代码
·遍历设备管理器的设备-.NET教程,评论及其它
·用正则表达式剔除文本中的HTML标记-ASP教程,正则表达式
·一个通用的DataGridTableStyle的做法-.NET教程,数据库应用
·java连接Oracle数据库-JSP教程,Java技巧及代码
·将XML存入关系数据库-JSP教程,数据库相关
·如何在Web页面上直接打开、编辑、创建Office文档-ASP教程,ASP应用
·asp之日期和时间函数示例-ASP教程,ASP应用
·ASP.Net Web Page深入探讨-ASP教程,ASP应用
·浅析Microsoft .net PetShop程序中的购物车和订单处理模块(Profile技术,异步MSMQ消息)-.NET教程,.NET Framework
最新文章
·fireworks 8绘制精致指南针图案_fireworks教程
·卸载多重引导系统中的windows vista操作系统_windows vista
·如何做到google adsense好收入的几点_网赚技巧
·百度主题推广和google adsense的综合比较_网赚技巧
·[新闻会客厅]孙雁:八零后的女闪客_站长访谈
·“流量交换型站点”访客黏度问题凸显_站长心得
·大唐社区站长经验谈社区运营_站长心得
·blog站点如何用rss搜索来推广_站长心得
·自我防护web站点和恶意链接的方法_站长心得
·网站投资你和我的20个自身检查(2)_站长心得
相关主题
  • 一个通用的分页类_asp.net技巧
  • 一个通用的表单验证程序-网页设计,HTML/CSS
  • 一个通用的保护ASP系统的方法-ASP教程,安全加密
  • 一个通用的连接池Bean-JSP教程,资料/其它
  • 一个通用的Datagrid导出Excel打印的源函数-.NET教程,数据库应用
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭