使用decimal更改DataTable的数据类型

时间:2014-03-06 10:41:14

标签: c# sorting types datatable type-conversion

我正在比较两个Datatables并构建了一个新表 我想对新表中的值进行排序,因为它有-ve值(如果没有转换为十进制,那么将不考虑-ve符号) 我想从字符串将其转换为Decimal类型并返回表进行排序。我收到错误,因为输入字符串格式不正确如何解决这个问题?并按照Asc顺序排序-ve值

private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
    DataTable table3 = new DataTable();
    DataRow dr = null;
    string filterExp = string.Empty;
    for (int i = 0; i < table1.Rows.Count; i++)
    {

        string col = table1.Rows[i]["Parameter Name"].ToString();
        if (table2.Columns.Contains(col))
        {
            if (!table3.Columns.Contains(col))
            {
                table3.Columns.Add(col, typeof(string));
                filterExp = filterExp + col + " asc ,";
            }

            for (int j = 0; j < table2.Rows.Count; j++)
            {
                if (table3.Rows.Count != table2.Rows.Count)
                {
                    dr = table3.NewRow();
                    table3.Rows.Add(dr);
                }
                table3.Rows[j][col] = (table2.Rows[j][col].ToString());
            }


        }


    }
    DataView dv = new DataView(table3);
    filterExp = filterExp.TrimEnd(',');
    dv.Sort = filterExp;
    table3 = dv.ToTable();

    return table3;
}

1 个答案:

答案 0 :(得分:0)

`

    private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)

       {

        DataTable table3 = new DataTable();
        DataRow dr = null;
        string filterExp = string.Empty;
        for (int i = 0; i < table1.Rows.Count; i++)
        {

            string col = table1.Rows[i]["Par Name"].ToString();
            if (table2.Columns.Contains(col))
            {
                if (!table3.Columns.Contains(col))
                {
                    table3.Columns.Add(col, typeof(double));
                    filterExp = filterExp + col + " asc ,";
                }

                for (int j = 0; j < table2.Rows.Count; j++)
                {
                    if (table3.Rows.Count != table2.Rows.Count)
                    {
                        dr = table3.NewRow();
                        table3.Rows.Add(dr);
                    }
                    table3.Rows[j][col] = table2.Rows[j][col];
                }


            }


        }

`