简体   繁体   中英

Formatting DataTable columns when adding data by rows

I am aware of and have used column formatting for a DataTable("dt") when adding columns, as in, eg:

dt.Columns.Add("myColName", GetType(Double))

or

DataColumn column;    
column = New DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "MyColName";
dt.Columns.Add(column);

However, I am adding data from a double array, x(i,j), to dt, but adding by row. How would I specify GetType(Double) to ensure the columns end up as Double?

Dim workrow As DataRow
For i = 0 To MyRows - 1
    workrow = dt.NewRow()
    For j = 0 To MyCols - 1
        workrow(j) = x(i, j)
    Next
    dt.Rows.Add(workrow)
Next

This was resolved by specifying column formats before data were added to the dt.

For j = 0 To dt.Columns.Count - 1
  dt.Columns(j).DataType = System.Type.GetType("System.Double")
Next

Although the array was a Double, and there would be no issues, this is nevertheless a way to format columns when data are added to dt by rows (not columns).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM