sql - VB.NET How do I apply decimal formatting to a specific column in a Gridview? -


how apply decimal formatting specific column in gridview?

eg 83.7837837837838 being populated sql, how convert 83.8.

i want apply 1 column other columns integers, won't necessary.

one way using dataformatstring property. example:

 <asp:gridview id="gridview1" runat="server"      autogeneratecolumns="false"      datakeynames="productid" datasourceid="sqldatasource1">     <columns>         <asp:boundfield datafield="listprice"              headertext="listprice"              sortexpression="listprice"             dataformatstring="{0:f1}" />     </columns> </asp:gridview> 

you use rowdatabound have more control:

protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) {     if(e.row.rowtype == datacontrolrowtype.datarow)     {         // assuming using boundfields ,         // column want format first         // if using templatefields use e.row.findcontrol("controlid") find controls         var row = (datarowview) e.row.dataitem;         decimal price = (decimal)row["listprice"];         e.row.cells[0].text = price.tostring("f1");     } } 

edit: here vb.net version:

protected sub gridview1_rowdatabound(sender [object], e gridviewroweventargs) handles gridview1.rowdatabound     if e.row.rowtype = datacontrolrowtype.datarow         ' assuming using boundfields ,                                                   '         ' column want format first                                         '         ' if using templatefields use e.row.findcontrol("controlid") find controls '         dim row = directcast(e.row.dataitem, datarowview)         dim price decimal = cdec(row("listprice"))         e.row.cells(0).text = price.tostring("f1")     end if end sub 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -