public class mybuttondatagridtablestyle1
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
public delegate sub datagridcellbuttonclickeventhandler(byval sender as object, byval e as datagridcellbuttonclickeventargs)
public event cellbuttonclicked as datagridcellbuttonclickeventhandler
private m_face as bitmap
private m_facepressed as bitmap
private m_columnnum as integer
private m_row as integer
public sub new(byval colnum as integer)
m_columnnum = colnum
m_row = -1
try
dim strm as system.io.stream = me.gettype().assembly.getmanifestresourcestream("btnface.bmp")
m_face = new bitmap(strm)
strm = me.gettype().assembly.getmanifestresourcestream("btnpressed.bmp")
m_facepressed = new bitmap(strm)
catch
end try
end sub
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)
end sub
public sub handlemouseup(byval sender as object, byval e as mouseeventargs)
dim dg as datagrid = me.datagridtablestyle.datagrid
dim hti as datagrid.hittestinfo = dg.hittest(new point(e.x, e.y))
dim isclickincell as boolean = hti.column = me.m_columnnum
m_row = -1
dim rect as new rectangle(0, 0, 0, 0)
if isclickincell then
rect = dg.getcellbounds(hti.row, hti.column)
isclickincell = e.x > rect.right - me.m_face.width
end if
if isclickincell then
dim g as graphics = graphics.fromhwnd(dg.handle)
g.drawimage(me.m_face, rect.right - me.m_face.width, rect.y)
g.dispose()
raiseevent cellbuttonclicked(me, new datagridcellbuttonclickeventargs(hti.row, hti.column))
end if
end sub
public sub handlemousedown(byval sender as object, byval e as mouseeventargs)
dim dg as datagrid = me.datagridtablestyle.datagrid
dim hti as datagrid.hittestinfo = dg.hittest(new point(e.x, e.y))
dim isclickincell as boolean = hti.column = me.m_columnnum
dim rect as new rectangle(0, 0, 0, 0)
if isclickincell then
rect = dg.getcellbounds(hti.row, hti.column)
isclickincell = e.x > rect.right - me.m_face.width
end if
if isclickincell then
dim g as graphics = graphics.fromhwnd(dg.handle)
g.drawimage(me.m_facepressed, rect.right - me.m_facepressed.width, rect.y)
g.dispose()
m_row = hti.row
end if
end sub
'重绘
protected overloads overrides sub paint(byval g as system.drawing.graphics, byval bounds as system.drawing.rectangle, byval [source] as system.windows.forms.currencymanager, byval rownum as integer, byval backbrush as system.drawing.brush, byval forebrush as system.drawing.brush, byval aligntoright as boolean)
dim parent as datagrid = me.datagridtablestyle.datagrid
'如果该行是选中行 或者 当前单元格的行号=点击行的行号并且当前单元格的列号等于new的列号参数
dim current as boolean = parent.isselected(rownum) or (parent.currentrowindex = rownum and parent.currentcell.columnnumber = me.m_columnnum)
dim backcolor as color
if current then backcolor = parent.selectionbackcolor else backcolor = parent.backcolor
dim forecolor as color
if current then forecolor = parent.selectionforecolor else forecolor = parent.forecolor
'请空单元格
g.fillrectangle(new solidbrush(backcolor), bounds)
' 绘制值
dim s as string = me.getcolumnvalueatrow([source], rownum).tostring() 'parent[rownum, 0].tostring() + ((parent[rownum, 1].tostring())+ " ").substring(0,2);
g.drawstring(s, parent.font, new solidbrush(forecolor), bounds.x, bounds.y)
dim bm as bitmap
if m_row = rownum then bm = me.m_facepressed else bm = me.m_face
g.drawimage(bm, bounds.right - bm.width, bounds.y)
end sub
end class
调用代码:
private function getdatagridstyle(byval table as datatable) as datagridtablestyle
dim style as new datagridtablestyle()
style.mappingname = table.tablename
style.rowheaderwidth = 15
dim i as integer
for i = 0 to table.columns.count - 1
if i = 1 then
dim textbuttoncolstyle as new mybuttondatagridtablestyle1(i) 'pass the column#
textbuttoncolstyle.headertext = table.columns(i).columnname
textbuttoncolstyle.mappingname = table.columns(i).columnname
'hookup our cellbutton handler...
addhandler textbuttoncolstyle.cellbuttonclicked, addressof handlecellbuttonclick
style.gridcolumnstyles.add(textbuttoncolstyle)
'hook the mouse handlers
addhandler datagrid1.mousedown, addressof textbuttoncolstyle.handlemousedown
addhandler datagrid1.mouseup, addressof textbuttoncolstyle.handlemouseup
else
dim c as new datagridtextboxcolumn()
c.headertext = table.columns(i).columnname
c.mappingname = table.columns(i).columnname
style.gridcolumnstyles.add(c)
end if
next
return style
end function
private sub handlecellbuttonclick(byval sender as object, byval e as datagridcellbuttonclickeventargs)
messagebox.show(("row " + e.rowindex.tostring() + " col " + e.colindex.tostring() + " clicked."))
end sub