문제

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?

도움이 되었습니까?

해결책

Make a Computed Column so that your data integrity never violated

You can see this article to find out How

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top