Question

I have a data-table with data. I need to add another column to the datatable having the same value of another column but with less precision.

Ie the original column would be having value 12.123 but the new column will have value 12.12

What is the best way to do this?

Was it helpful?

Solution

Make a Computed Column so that your data integrity never violated

You can see this article to find out How

OTHER TIPS

Something like this

            yourDataTable.Columns.Add("newCol", typeof(double));

            foreach (System.Data.DataRow row in yourDataTable.Rows)
            {
                row["newCol"] = Math.Round(Convert.ToDouble(row["oldCol"]), 2);                    
            }

Some grid implementations, like DevExpress XtraGrid, allow you to have unbound columns and easily provide values for them. The same thing would involve some hacking with a basic DataGridView, so I have an other suggestion. Add a new read-only property to your class: decimal value1 { get; set; } decimal value2 { get { return Math.Round(value1, 2); } } This way you don't even have to bother with the column display format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top