c# - Displaying 2 Decimal places in Datagridview without rounding off -
i have datagridview
allows user input prices of product. client wants if enter 2.525 in price, display 2.52 , when focus or edit cell should display full vale of 2.525.
i using code in formatting datagridview
columns 1 rounding off entered value , not display whole value when cell in editing mode because using c2.
custinvlines.columns["invlineprice"].defaultcellstyle.format = c2
how format cell display whole value entered user in edit mode , display 2 decimal places after editing?
you should format value in datagridview.cellformatting
event handler
private void custinvlines_cellformatting(object sender, datagridviewcellformattingeventargs e) { if (e.rowindex < 0) { return; } var column = custinvlines.columns[e.columnindex]; if (column.name == "invlineprice") { var cell = custinvlines.rows[e.rowindex].cells[e.columnindex]; var value = (decimal)e.value; if (cell.isineditmode) { e.value = value.tostring(); // display digits } else { // display 2 digits of decimals without rounding var twodigitsvalue = math.truncate(value * 100)/100; e.value = twodigitsvalue.tostring("c2"); } } }
Comments
Post a Comment