Click here to Skip to main content
14,738,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Frnds,
I wan to add % symbol as suffix in percentage column value in datatable in C# code.

What I have tried:

I already tried with:

foreach (DataRow drow in dt.Rows)
drow["PER"] = string.Concat(drow.Field<decimal>("PER"), "%");

Here my PER column was in decimal value . I can't concat the values. Anyone please help to resolve the issue.

Thanks in Advance.
Updated 22-Mar-18 22:22pm

Don't try to change your DataTable - that's a bad idea as it may be linked to the data source. And adding a "%" to the source would mean changing it from a numeric value to a string, which would be a very bad idea!

Instead, look at formatting the data when you present it to the user - DataGridView and suchlike all have formatting options which would allow you to display the percentage sign you require. I can't tell you what to do - I have no idea what you are using the DataTable with, or even the environment you are presenting data in - but the MSDN documentation should be able to help if you Google the control name and "Formatting".
You can't simply add [%] right after the number. A PER column is type of numeric (probably decimal), so it can't store a text!

To resolve it, you can add computed/expression column[^]. See:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("PER", typeof(decimal)));
dt.Rows.Add(new object[]{0.85});
dt.Rows.Add(new object[]{0.99});
dt.Rows.Add(new object[]{0.67});
dt.Rows.Add(new object[]{0.46});
dt.Rows.Add(new object[]{0.27});

DataColumn dc = new DataColumn("PER2", typeof(string));
dc.Expression = "(PER*100) + '%'";

0,85 85,00% 
0,99 99,00% 
0,67 67,00% 
0,46 46,00% 
0,27 27,00%

For further details, please see: DataColumn.Expression Property (System.Data)[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900