We have applied dataformat string to column to change the display cell value for negative number to be represented as (123)
column1.DataFormatString ="{0:#,##0.00;(#,##0.00);0.00}";
the display of column works fine. But when cell is edited for which DataFormatString is applied Javascript error is thrown.
Microsoft JScript runtime error: Sys.FormatException: Format specifier was invalid.
Can you please help to apply Format string for displaying negative numbers.
Hi Abhijit,
Thank you for posting in the community.
The WebDataGrid does not support Custom Numeric Format Strings, but only Standard Numeric Format Strings.
However, the formatting that you want to implement is very close to the Currency Negative Pattern.
If you choose to use it, just use the following format string: "{0:C2}" . Keep in mind that this pattern, requires the presence of a currency sign.
Otherwise you can implement some custom solution, depending on the requirements of your project.
Please feel free to contact me if you have any further questions.
Thanks Petko for your answer. We do no want currency sign to be present in formatted value. Can you please let me know how custom solution can be implemented in this case.
Hello Abhijit,
If you want to implement some custom solution, may be you should look first at this thread: Set DataFormatString in server-side code for a specific cell .
Also I think the following pieces of code will be helpful too:
C#:
JS:
You can hook the JS function as a handler of the WebDataGrid client event CellEditing and this is everything you need to accomplished the behavior you expected.
I also prepared and attached small sample that demonstrate how to implement your custom solution.
I hope this solution will help you.
I tried adding method for FormatFieldMethod. But seems like the method is not getting invoked and no formatting applied to cells. Also second option of adding Javscript method is not feasible as it will apply formatting only when editing cell. We want display and edit cell value in same format.
protected override void OnInit(EventArgs e){
base.OnInit(e);
grdDetail.Load += (grid, arg) =>{
var col = grdDetail.Columns["TOTAL_SUM"
];
col.FormatFieldMethod = (field, value) =>
{
double
val;
if (double.TryParse(value.ToString(), out
val))
if
(val < 0)
return string.Format("({0:N2})"
, -1 * val);
}
else
return string.Format("{0:N2}"
, val);
return "Value cannot be convert to double"
;
};
Let me clarify my previous response.
The C# code should handle the cases where a cell is displaying in a custom way.
The JS code should handled the cases where a cell is editing in a custom way.
I see in your adapted code that you've changed "((WebDataGrid)grid)" with "grdDetail" in the expression. Please undo this change, because the lambda expression needs to work with parameter "grid" instead of "grdDetail".
Probably this breaks the C# code and that is why only editing is formatted as you expected.
I hope my answer will help to resolve your issue. I will keep in touch.