0

我想在存储在 a 中的值中添加字符(如%和) ,但我收到一条错误消息:ppDataTable

输入字符串的格式不正确。无法在实际列中存储 <97.0%>。预期类型为十进制。

如何更改DataTable可以存储的值的类型?

Dim dv As New System.Data.DataView
Dim dt As New System.Data.DataTable
dv = SQL_Customer.Select(DataSourceSelectArguments.Empty)
dt = dv.ToTable()

dt.Rows(1)(1) = CStr(dt.Rows(1)(1)) & "%"
dt.Rows(1)(2) = CStr(dt.Rows(1)(2)) & "%"
dt.Rows(1)(3) = CStr(dt.Rows(1)(3)) & "%"
dt.Rows(1)(4) = CStr(dt.Rows(1)(4)) & "pp"
4

2 回答 2

0

一旦填充数据表列,您将无法更改其类型。

一种解决方法是添加一个字符串类型的新列,然后用您需要的字符串值填充该列,然后删除您不需要的类型的列。

于 2013-03-20T20:05:00.127 回答
0

如果这对某人有帮助,这就是我解决问题的方法:

    Dim dv As New System.Data.DataView
    Dim dt As New System.Data.DataTable
    Dim dt2 As New System.Data.DataTable
    Dim SelectedIndex As Object

    If datagrid.SelectedIndex = "-1" Then SelectedIndex = 0 Else SelectedIndex = GV_Cust.SelectedIndex

    'Populate Dataview with data in SQL Query for Customer KPIs
    dv = SQL_Customer.Select(DataSourceSelectArguments.Empty)
    'Populate table with data from dataview. Note that the data is formated as per data loaded. So if number is loaded, then format for the cell is number. This creates a problem when adding percentages
    dt = dv.ToTable()

    'New colums are added as string format in the dataTable so that % and pp can be added
    Dim column As DataColumn
    For i = 1 To 6
        ' Create second column. 
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = i
        column.AutoIncrement = False
        column.Caption = i
        column.ReadOnly = False
        column.Unique = False
        ' Add the Column to the DataColumnCollection. 
        dt.Columns.Add(column)
    Next

    'New columns are populated and calculations for variances are calculated
    For j = 0 To 2
        For i = 0 To 4
            dt.Rows(i)(7 + j) = dt.Rows(i)(1 + j) & "%"
            dt.Rows(i)(10) = (dt.Rows(i)(1) - dt.Rows(i)(2)) & "pp"
            dt.Rows(i)(11) = (dt.Rows(i)(1) - dt.Rows(i)(3)) & "pp"
            dt.Rows(i)(12) = (dt.Rows(i)(2) - dt.Rows(i)(3)) & "pp"
        Next
        For i = 5 To 6
            dt.Rows(i)(7 + j) = dt.Rows(i)(1 + j)
            If dt.Rows(i)(2) = 0 Then dt.Rows(i)(10) = 0 & "%" Else dt.Rows(i)(10) = Math.Round((((dt.Rows(i)(1) / dt.Rows(i)(2))) - 1) * 100, 1) & "%"
            If dt.Rows(i)(3) = 0 Then dt.Rows(i)(11) = 0 & "%" Else dt.Rows(i)(11) = Math.Round((((dt.Rows(i)(1) / dt.Rows(i)(3))) - 1) * 100, 1) & "%"
            If dt.Rows(i)(3) = 0 Then dt.Rows(i)(12) = 0 & "%" Else dt.Rows(i)(12) = Math.Round((((dt.Rows(i)(2) / dt.Rows(i)(2))) - 1) * 100, 1) & "%"
        Next
    Next

    'Old columns are deleted and new wones are renamed
    dt.Columns.Remove("A")
    dt.Columns("1").ColumnName = "A"
    dt.Columns.Remove("B")
    dt.Columns("2").ColumnName = "B
    dt.Columns.Remove("C")
    dt.Columns("3").ColumnName = "C"
    dt.Columns.Remove("D")
    dt.Columns("4").ColumnName = "D"
    dt.Columns.Remove("D")
    dt.Columns("5").ColumnName = "E"
    dt.Columns.Remove("F")
    dt.Columns("6").ColumnName = "F"

    'Customer grid is populated, a selection button is added and the first row is selected
    datagrid.DataSource = dt
    datagrid.AutoGenerateSelectButton = True
    datagrid.DataBind()
    datagrid.SelectedIndex = SelectedIndex
于 2013-03-22T14:27:53.410 回答