商业源码热门下载www.html.org.cn
在前面看到了很多关于怎样向datagrid中添加combobox控件的方法。使用的方法全部都是在vb6.0中的方法。
我还是要说说在csnd中发贴的朋友。
现在所谓的.net编程人员,不知道是怎么了呢!只是停留在使用.net的编程环境中。并没有真正的了解面向对象的.net编程思想。
我现在就利用继承datagridcolumnstyle完成向datagrid中添加combobox。
希望这样有助于大家了解真正的面向对象编程的思想。不要只是认为利用vb6.0中的某些方法就是.net高手了。有这种思想的人都是菜鸟 (希望这么说没有得罪太多的朋友:)下面就是实现的代码:我使用的是vb.net来完成的。
我熟悉c#,但是vb.net只是大概了解一下。应该比一些人要高一点点吧!:)
见笑了!由于时间关系没有协注释,请见谅!
public class datagridcombocolumn
inherits datagridcolumnstyle
public withevents dgcombo as combobox = new combobox
private isediting as boolean
private _strselectedtext as string
public sub new()
mybase.new()
dgcombo.visible = false
end sub
protected overrides sub abort(byval rownum as integer)
isediting = false
removehandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
invalidate()
end sub
protected overrides function commit(byval datasource as system.windows.forms.currencymanager, byval rownum as integer) as boolean
dgcombo.bounds = rectangle.empty
addhandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
if isediting = false then
return true
end if
isediting = false
try
dgcombo.text = dgcombo.text
catch ex as exception
dgcombo.text = string.empty
end try
try
dim value as string = _strselectedtext
setcolumnvalueatrow(datasource, rownum, value)
catch ex as exception
abort(rownum)
return false
end try
invalidate()
return true
end function
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)
dim value as string
try
value = ctype(getcolumnvalueatrow(source, rownum), string)
catch ex as exception
setcolumnvalueatrow(source, rownum, dgcombo.text)
end try
value = ctype(getcolumnvalueatrow(source, rownum), string)
if (cellisvisible) then
dgcombo.bounds = new rectangle(bounds.x, bounds.y, bounds.width, bounds.height)
dgcombo.text = value
dgcombo.visible = true
addhandler dgcombo.selectedvaluechanged, addressof dgcombo_selectedvaluechanged
else
dgcombo.text = value
dgcombo.visible = false
end if
if dgcombo.visible = false then
datagridtablestyle.datagrid.invalidate(bounds)
end if
end sub
protected overrides function getminimumheight() as integer
end function
protected overrides function getpreferredheight(byval g as system.drawing.graphics, byval value as object) as integer
end function
protected overrides function getpreferredsize(byval g as system.drawing.graphics, byval value as object) as system.drawing.size
end function
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)
paint(g, bounds, source, rownum, true)
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 aligntoright as boolean)
paint(g, bounds, source, rownum, brushes.red, brushes.blue, aligntoright)
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 strdate as string
dim rect as rectanglef
try
strdate = dgcombo.text
strdate = ctype(getcolumnvalueatrow(source, rownum), string)
catch ex as exception
setcolumnvalueatrow(source, rownum, dgcombo.text)
strdate = ctype(getcolumnvalueatrow(source, rownum), string)
end try
rect.x = bounds.x
rect.y = bounds.y
rect.height = bounds.height
rect.width = bounds.width
g.fillrectangle(backbrush, rect)
rect.offset(0, 2)
rect.height -= 2
g.drawstring(strdate, me.datagridtablestyle.datagrid.font, forebrush, rect)
end sub
protected overrides sub setdatagridincolumn(byval value as datagrid)
mybase.setdatagridincolumn(value)
if not (dgcombo.parent is nothing) then
dgcombo.parent.controls.remove(dgcombo)
end if
if not (value is nothing) then
value.controls.add(dgcombo)
end if
end sub
private sub dgcombo_selectedvaluechanged(byval sender as object, byval e as system.eventargs) handles dgcombo.selectedvaluechanged
isediting = true
mybase.columnstartedediting(dgcombo)
_strselectedtext = dgcombo.text
if _strselectedtext is nothing then
_strselectedtext = string.empty
end if
end sub
end class
以下是使用方法!
private sub frmdatagrid_load(byval sender as object, byval e as system.eventargs) handles mybase.load
call test()
end sub
private sub test()
dim dt as new datatable("test")
dt.columns.add(new datacolumn("id"))
dt.columns.add(new datacolumn("combo"))
call addgridstyle()
call adddata(dt)
dbgrid.datasource = dt
end sub
private sub addgridstyle()
dim dbgridstyle as datagridtablestyle
dim idcolumn as datagridtextboxcolumn
dim dccolumn as datagridcombocolumn
dbgridstyle = new datagridtablestyle
dbgridstyle.mappingname = "test"
idcolumn = new datagridtextboxcolumn
idcolumn.mappingname = "id"
idcolumn.headertext = "id"
idcolumn.alignment = horizontalalignment.center
idcolumn.width = 50
dbgridstyle.gridcolumnstyles.add(idcolumn)
dccolumn = new datagridcombocolumn
dim i as integer
for i = 0 to 25
dccolumn.dgcombo.items.add((i + 1).tostring("00000"))
next
dccolumn.dgcombo.dropdownwidth = 120
dccolumn.mappingname = "combo"
dccolumn.headertext = "combo"
dccolumn.alignment = horizontalalignment.center
dccolumn.width = 60
dbgridstyle.gridcolumnstyles.add(dccolumn)
dbgrid.tablestyles.add(dbgridstyle)
end sub
private sub adddata(byref dt as datatable)
dim drow as datarow
dim introw as integer
for introw = 0 to 9
drow = dt.newrow()
drow.item("id") = format(introw + 1, "000")
drow.item("combo") = format(introw + 1, "00000")
dt.rows.add(drow)
next
dt.acceptchanges()
end sub
那么随时在datagrid中所指定为combo列中单击。combo就出现了
新闻热点
疑难解答