一个通用的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