asp.net - Change cell color on Gridview based on condition using C# -


i trying change color of cell if balance less 0 make red. getting error:

input string not in correct format. 

and here gridview

<asp:gridview id="gvtest" runat="server" width="700px" cssclass="table table-hover table-bordered table-responsive" onrowdatabound = "onrowdatabound"                         autogeneratecolumns="false" datakeynames="id"                         allowpaging="true"                         onpageindexchanging="onpaging_gvbookkeeping"                         pagesize="25">                         <columns>                           <asp:boundfield datafield="id" headertext="id" htmlencode="true" />                           <asp:boundfield datafield="fullname" headertext="name" htmlencode="true" />                            <asp:boundfield datafield="remaining_ballance" dataformatstring="{0:c0}" headertext="remaining ballance" htmlencode="true" />                           <asp:boundfield datafield="note" headertext="note" htmlencode="true" />                           <asp:boundfield datafield="fully_paid" headertext="fully paid" htmlencode="true" />                            <asp:templatefield itemstyle-width="30px" headertext="edit link">                             <itemtemplate>                               <asp:linkbutton id="lnkedit" causesvalidation="false" runat="server" text="edit" onclick="edit"></asp:linkbutton>                             </itemtemplate>                           </asp:templatefield>                         </columns>                         <headerstyle backcolor="#e5e5e5" />                          <pagersettings position="topandbottom" />                         <pagerstyle backcolor="#cccccc" forecolor="#ff3300" horizontalalign="center" />                       </asp:gridview> 

here code behind

protected void onrowdatabound(object sender, gridviewroweventargs e) {    if (e.row.rowtype == datacontrolrowtype.datarow)   {     tablecell cell = e.row.cells[2];     int ballance = int.parse(cell.text);     if (ballance < 0)     {       cell.backcolor = color.red;     }   } } 

it failing @ line

int ballance = int.parse(cell.text); 

the data type balance column decimal(10,2)

it looks should have tablecell cell = e.row.cells[2]; rather tablecell cell = e.row.cells[3];

column indexes start @ 0, rather 1.

in case, means trying parse value of notes field int.


edit

you may find dataformatstring="{0:c0}" formats number currency sign (eg $/£) , character cannot converted integer.

adding breakpoint suggested identify problem.


Comments