c# - ASP.NET GridView use FindControl() on BoundField to manipulate field -


i'm working old app had hard coded columns different locations, new locations being added decided try , make things populate dynamically. 1 of features of app displaying red text , bolding text when status deemed "bad". executed using "findcontrol()" function cells within selected row templatefields.

now i've set use bound field, how go changing text color, size, etc. during databound event?

boundfield being added gridview

        boundfield statusfield = new boundfield();         statusfield.datafield = "exceptionstatuscode";         statusfield.headertext = "status";         statusfield.sortexpression = "exceptionstatuscode";         this.gvview.columns.add(statusfield); 

databound event gridview

    protected void gvview_databound(object sender, eventargs e)     {         foreach (gridviewrow row in this.gvview.rows)         {             //no longer works, need know how reproduce             //what below bound field             label lblpartstatus = ((label) row.cells[statuscolumn].findcontrol("lblpartstatus"));             if (lblpartstatus.text == "bad")             {                 lblpartstatus.forecolor = system.drawing.color.red;                 row.tooltip = "one or more locations missing information!";                 row.backcolor = system.drawing.color.salmon;             }         }     } 

years ago wrote code find cell index using column's sortexpression, headertext or datafield. it's saved me lots of effort on years, , call myrow.cells[myrow.getcellindexbyfieldhandle(mydatafieldname)]


public static class utility {     /// <summary>     /// gets ordinal index of tablecell in rendered gridviewrow, using text fieldhandle (e.g. corresponding column's datafieldname/sortexpression/headertext)     /// </summary>     public static int getcellindexbyfieldhandle(this gridview grid, string fieldhandle)     {         int icellindex = -1;          (int icolindex = 0; icolindex < grid.columns.count; icolindex++)         {             if (grid.columns[icolindex] datacontrolfield)             {                 datacontrolfield col = (datacontrolfield)grid.columns[icolindex];                 if ((col boundfield && string.compare(((boundfield)col).datafield, fieldhandle, true) == 0)                     || string.compare(col.sortexpression, fieldhandle, true) == 0                     || col.headertext.contains(fieldhandle))                 {                     icellindex = icolindex;                     break;                 }             }         }         return icellindex;     }      /// <summary>     /// gets ordinal index of tablecell in rendered gridviewrow, using text fieldhandle (e.g. corresponding column's datafieldname/sortexpression/headertext)     /// </summary>     public static int getcellindexbyfieldhandle(this gridviewrow row, string fieldhandle)     {         return getcellindexbyfieldhandle((gridview)row.parent.parent, fieldhandle);     } } 

once have cell, suggest manipulate setting cell.cssclass, , using css style accordingly. steer clear of inline style, when set forecolor, backcolor etc.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -