c# - Draw red line inside datagridview cells -


how can add red line cell?

    private void datagridview_cellpainting(object sender, datagridviewcellpaintingeventargs e)     {         var dg = (datagridview)sender;          if (e.columnindex == -5 || e.rowindex != (dg.rowcount -1))             return;          using (var p = new pen(color.red, 1))         {             var cellbounds = e.cellbounds;              const int size = 2;             var pts = new list<point>();             var h = false;             (int = cellbounds.left; <= cellbounds.right; += size, h = !h)             {                 pts.add(                     new point                     {                         x = i,                         y = h ? cellbounds.bottom : cellbounds.bottom + size                     });             }              e.graphics.drawlines(p, pts.toarray());         }     }      private void datagridview_paint(object sender, painteventargs e)     {        datagridview.cellpainting += datagridview_cellpainting;     } 

there few issues code:

  • all points create make no sense @ all. draw line need 2 points , series of n lines series n+1 points.

  • do not add more , more handlers cellpainting! each call of datagridview_paint adds 1 more. should deleted. add handler in designer!

  • the cellpainting event has few rules follow:

    • if handle drawing need handle all of it, including drawing background , content
    • to make life (a lot) easier there ready calls this, right in param e
    • if want handle drawing need abort system drawing setting e.handled = true;

here code draws red line under each cell, including rowheader:

private void datagridview1_cellpainting(object sender, datagridviewcellpaintingeventargs e) {     var dg = (datagridview)sender;  // short reference     if (e.columnindex == -5 || e.rowindex != (dg.rowcount - 1))         return;      using (var p = new pen(color.red, 1))     {         var cb = e.cellbounds;  // short reference          e.paintbackground(e.clipbounds, true);         e.paintcontent(e.clipbounds);         e.graphics.drawline(p, cb.x, cb.y + cb.height, cb.x + cb.width, cb.y + cb.height);          e.handled = true;     } }  

i left in, wonder e.columnindex == -5 meant do; maybe typo? exclude rowheader change e.columnindex < 0 or else leave out!


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -