问题
I have this code to add new column to datatable:
DataColumn col = new DataColumn("column", typeof(decimal)); col.Caption = "Column"; mytable.Columns.Add(col);
How can I specify decimal precision for this column so the value will always be in the format I want it to be?
回答1:
You can not. However, you can format the value when you retrieve it from the table using String.Format
function:
String.Format("{0:0.##}", (Decimal) myTable.Rows[rowIndex].Columns[columnIndex]);
回答2:
I had this same problem myself, and I fixed it by loading the whole schema on app startup, and then referencing the column information from the schema as I needed it.
I only have a Visual Basic example, but hopefully it's pretty easy to translate to C#
Setup
' in whatever class you do your database communication:
Private _database As SqlDatabase
Private Shared _schema As DataTable
Sub New()
' or however you handle the connection string / database creation
Dim connectionString as String = GetConnectionString()
_database = New SqlDatabase(connectionString)
RetrieveSchema()
End Sub
Private Function RetrieveSchema() as DataTable
If _schema Is Nothing Then
Using connection As SqlConnection = _database.CreateConnection()
connection.Open()
_schema = connection.GetSchema("Columns")
End Using
End If
return _schema
End Function
Public Function GetColumnInformation(tableName As String, columnName As String) as DataRow
Dim firstMatchingRow as DataRow = (
From row In _schema.Rows _
Where (
row("TABLE_NAME") = tableName AndAlso row("COLUMN_NAME") = columnName)
)).FirstOrDefault()
Return firstMatchingRow
End Function
Usage
Dim columnInformation As DataRow = Dal.GetColumnInformation(tableName, columnName)
' find the precision
Dim precision = columnInformation("NUMERIC_PRECISION")
Dim scale = columnInformation("NUMERIC_SCALE")
' convert the decimal to the column's format
' e.g.: 2.345 with a scale of 2 would result in
' 2.35
value = Decimal.Round(value, scale)
来源:https://stackoverflow.com/questions/6176657/c-sharp-datatable-decimal-with-precision