C#重写DataGridView
程序员文章站
2022-06-11 14:23:20
本文实例为大家分享了c#重写datagridview的实例代码,供大家参考,具体内容如下
using system;
using system.co...
本文实例为大家分享了c#重写datagridview的实例代码,供大家参考,具体内容如下
using system; using system.collections.generic; using system.text; using system.drawing; using system.windows.forms; using system.drawing.drawing2d; namespace crd.winui.editors { public class datagridview : system.windows.forms.datagridview { private bool _cellcoloronchange=false; private color cell_color = color.yellow; private bool shifouhuasanjiao = true; private color color_grid = color.fromargb(236, 233, 216); bool click = false; public datagridview() { this.setstyle(controlstyles.doublebuffer | controlstyles.allpaintinginwmpaint, true); } protected override void oncreatecontrol() { this.enableheadersvisualstyles = false; this.columnheadersdefaultcellstyle.backcolor = color.fromargb(236, 233, 216); this.columnheadersborderstyle = datagridviewheaderborderstyle.raised; //this.columnheadersheight = 20; this.columnheadersheightsizemode = datagridviewcolumnheadersheightsizemode.disableresizing; this.columnheadersdefaultcellstyle.alignment = system.windows.forms.datagridviewcontentalignment.middlecenter; this.columnheadersdefaultcellstyle.forecolor = system.drawing.systemcolors.windowtext; this.columnheadersdefaultcellstyle.selectionbackcolor = system.drawing.systemcolors.highlight; this.columnheadersdefaultcellstyle.selectionforecolor = system.drawing.systemcolors.highlighttext; this.rowheadersdefaultcellstyle.alignment = system.windows.forms.datagridviewcontentalignment.middleleft; this.rowheadersdefaultcellstyle.backcolor = color.fromargb(236, 233, 216); this.rowheadersdefaultcellstyle.forecolor = system.drawing.systemcolors.windowtext; this.rowheadersborderstyle = system.windows.forms.datagridviewheaderborderstyle.raised; this.defaultcellstyle.selectionbackcolor = color.darkblue; this.defaultcellstyle.selectionforecolor = color.darkslateblue; this.rowheaderswidthsizemode = datagridviewrowheaderswidthsizemode.disableresizing; //this.gridcolor = color.silver;//表格点击后颜色 表格线颜色 this.backgroundcolor = system.drawing.systemcolors.window; this.borderstyle = system.windows.forms.borderstyle.fixed3d; this.allowusertoordercolumns = true; this.autogeneratecolumns = true; base.oncreatecontrol(); } color defaultcolor; //移到单元格时的颜色 protected override void oncellmousemove(datagridviewcellmouseeventargs e) { base.oncellmousemove(e); try { if (_cellcoloronchange) rows[e.rowindex].defaultcellstyle.backcolor = cell_color; } catch (exception) { } } //进入单元格时保存当前的颜色 protected override void oncellmouseenter(datagridviewcelleventargs e) { base.oncellmouseenter(e); try { if (_cellcoloronchange) defaultcolor = rows[e.rowindex].defaultcellstyle.backcolor; } catch (exception) { } } //离开时还原颜色 protected override void oncellmouseleave(datagridviewcelleventargs e) { base.oncellmouseleave(e); try { if (_cellcoloronchange) rows[e.rowindex].defaultcellstyle.backcolor = defaultcolor; } catch (exception) { } } public bool cellcoloronchange { get { return _cellcoloronchange; } set { _cellcoloronchange = value; } } public color defaultcolorset { get { return cell_color; } set { cell_color = value; } } public bool shifouhua_sanjiao { get { return shifouhuasanjiao; } set { shifouhuasanjiao = value; } } public color content_grid_color { get { return color_grid; } set { color_grid = value; } } private void initializecomponent() { ((system.componentmodel.isupportinitialize)(this)).begininit(); this.suspendlayout(); // // datagridview // //this.rowtemplate.height = 17; ((system.componentmodel.isupportinitialize)(this)).endinit(); this.resumelayout(false); } //rowpostpaint protected override void onrowpostpaint(datagridviewrowpostpainteventargs e) { if (shifouhuasanjiao) { using (solidbrush b = new solidbrush(color.black)) { image image = global::crd.winui.properties.resources.未标题_1; //e.graphics.drawstring("►", e.inheritedrowstyle.font, b, e.rowbounds.location.x + 5, e.rowbounds.location.y + 4); //e.graphics.drawimageunscaled(image, e.rowbounds.location.x + 1, e.rowbounds.location.y + 2, 8, 13); if (click) if (e.rowindex == this.currentrow.index) { e.graphics.drawimageunscaled(image, e.rowbounds.location.x + 1, e.rowbounds.location.y + 2, 8, 13); } } } base.onrowpostpaint(e); } protected override void onrowprepaint(datagridviewrowprepainteventargs e) { if (shifouhuasanjiao) { using (solidbrush b = new solidbrush(color.black)) { image image = global::crd.winui.properties.resources.未标题_1; //e.graphics.drawstring("►", e.inheritedrowstyle.font, b, e.rowbounds.location.x + 5, e.rowbounds.location.y + 4); //e.graphics.drawimageunscaled(image, e.rowbounds.location.x + 1, e.rowbounds.location.y + 2, 8, 13); } } base.onrowprepaint(e); } protected override void oncellclick(datagridviewcelleventargs e) { if (e.rowindex > -1&&this.currentrow.index == e.rowindex ) { click = true; } base.oncellclick(e); } protected override void oncellpainting(datagridviewcellpaintingeventargs e) { base.oncellpainting(e); solidbrush b = new solidbrush(color.fromargb(236, 233, 216)); pen whitepen = new pen(color_grid, 1); if (e.columnindex == -1 && e.rowindex == -1) { using (lineargradientbrush brush = new lineargradientbrush(e.cellbounds, color.gray, color.gray, lineargradientmode.forwarddiagonal)) { e.graphics.fillrectangle(b, e.cellbounds); rectangle border = e.cellbounds; border.offset(new point(-1, -1)); e.graphics.drawrectangle(pens.gray, border); } e.paintcontent(e.cellbounds); e.handled = true; } else if (e.rowindex == -1) { //标题行 using (lineargradientbrush brush = new lineargradientbrush(e.cellbounds, color.silver, color.silver, lineargradientmode.vertical)) { e.graphics.fillrectangle(b, e.cellbounds); rectangle border = e.cellbounds; border.offset(new point(-1, -1)); e.graphics.drawrectangle(pens.silver, border); //e.graphics.drawrectangle(pens.black, border.x + 1, border.y + 1, border.width - 1, border.height - 1); } e.paintcontent(e.cellbounds); e.handled = true; } else if (e.columnindex == -1) { //标题列 using (lineargradientbrush brush = new lineargradientbrush(e.cellbounds, color.silver, color.silver, lineargradientmode.horizontal)) { e.graphics.fillrectangle(b, e.cellbounds); rectangle border = e.cellbounds; border.offset(new point(-1, -1)); e.graphics.drawrectangle(pens.silver, border); //e.graphics.drawrectangle(pens.black, border.x+1,border.y+1,border.width-1,border.height-1); e.graphics.drawstring("△", font,b,e.cellbounds.x,e.cellbounds.y); } e.paintcontent(e.cellbounds); e.handled = true; } else { //color.fromargb(193, 193, 193) rectangle border = e.cellbounds; border.offset(new point(-1, -1)); e.graphics.drawrectangle(whitepen, border); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇: C#中Byte转换相关的函数
下一篇: 蛋清有蛋黄打不发补救的方法有哪些比较适用