问题描述
privatevoiddgvSMT_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){//定义字体FontMyFont=newFont("Arial",12,FontStyle.Bold);//定义字体颜色SolidBrushMdrawBrush=newSolidBrush(Color.White);//MainBoardSolidBrushSdrawBrush=newSolidBrush(Color.Black);//SensorBoard//横向全合并if(e.RowIndex==1||e.RowIndex==5||e.RowIndex==10||e.RowIndex==25){using(BrushgridBrush=newSolidBrush(this.dgvSMT.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush)){if(e.ColumnIndex>=0){//清除线条e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//绘右侧的线if(e.ColumnIndex==11){e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom-1);}//绘制值if(e.RowIndex==1&&e.ColumnIndex==0){e.Graphics.DrawString("MainBoard",MyFont,MdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==5&&e.ColumnIndex==0){e.Graphics.DrawString("SensorBoard",MyFont,SdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==10&&e.ColumnIndex==0){e.Graphics.DrawString("MainBoardFaillist",MyFont,MdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==25&&e.ColumnIndex==0){e.Graphics.DrawString("SensorBoardFaillist",MyFont,SdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}//下边缘的线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);e.Handled=true;}dgvSMT.Rows[1].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[5].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[10].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[25].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[4].DefaultCellStyle.BackColor=Color.FromArgb(255,255,0);dgvSMT.Rows[8].DefaultCellStyle.BackColor=Color.FromArgb(255,255,0);}}}}
会一直执行这个方法,死循环了,怎么才能只执行一次呢,??
解决方案
解决方案二:
自己定义一个标志,去判断这个标志,如果已绘制则改变状态为True,话说你为什么要求只执行一次呢,最好具体描述下你的需求
解决方案三:
你这代码哪里死循环了。。。一直执行cellpaint有可能是你单元格太多呗,每一个单元格都会触发这个事件
解决方案四:
引用楼主fz321的回复:
privatevoiddgvSMT_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){//定义字体FontMyFont=newFont("Arial",12,FontStyle.Bold);//定义字体颜色SolidBrushMdrawBrush=newSolidBrush(Color.White);//MainBoardSolidBrushSdrawBrush=newSolidBrush(Color.Black);//SensorBoard//横向全合并if(e.RowIndex==1||e.RowIndex==5||e.RowIndex==10||e.RowIndex==25){using(BrushgridBrush=newSolidBrush(this.dgvSMT.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush)){if(e.ColumnIndex>=0){//清除线条e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//绘右侧的线if(e.ColumnIndex==11){e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom-1);}//绘制值if(e.RowIndex==1&&e.ColumnIndex==0){e.Graphics.DrawString("MainBoard",MyFont,MdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==5&&e.ColumnIndex==0){e.Graphics.DrawString("SensorBoard",MyFont,SdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==10&&e.ColumnIndex==0){e.Graphics.DrawString("MainBoardFaillist",MyFont,MdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}if(e.RowIndex==25&&e.ColumnIndex==0){e.Graphics.DrawString("SensorBoardFaillist",MyFont,SdrawBrush,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}//下边缘的线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);e.Handled=true;}dgvSMT.Rows[1].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[5].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[10].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[25].DefaultCellStyle.BackColor=Color.FromArgb(0,128,0);dgvSMT.Rows[4].DefaultCellStyle.BackColor=Color.FromArgb(255,255,0);dgvSMT.Rows[8].DefaultCellStyle.BackColor=Color.FromArgb(255,255,0);}}}}会一直执行这个方法,死循环了,怎么才能只执行一次呢,??
这个用来做报表用。数据就比较大效率已经比较低了,再就是满屏的刷新亮瞎眼了
解决方案五:
引用2楼lc316546079的回复:
你这代码哪里死循环了。。。一直执行cellpaint有可能是你单元格太多呗,每一个单元格都会触发这个事件
+1