How to create datatable columns with DataType from Array?

advertisements

How to create new DataTable with column DataType from 2D Array? This is My Code:

DataTable dtColorList = new DataTable;

string[,] values = new string[,] { {"ID","int"}, {"Name","string"}, {"ColorID","int16"} };
AddDataTableColWithType(dtColorList, values);

public static void AddDataTableColWithType(DataTable dtName, string[,] colNameAndType)
    {
        for (int i = 0; i < colNameAndType.Length / 2; i++)
        {
            dtName.Columns.Add(colName[i,0],typeof(Type.GetType(colType[i,1])));

        }
    }

ERROR on Type.GetType


You can use this simple solution: The basic idea is using mapping

public static Dictionary<string, Type> PrimitiveTypes = new Dictionary<string, Type>
      {
         {"int",typeof(int)},
         {"string",typeof(string)}
      };

    public static void Main(string[] args)
    {
        DataTable dtColorList = new DataTable();

        string[,] values = new string[,] { { "ID", "int" }, { "Name", "string" } };
        AddDataTableColWithType(dtColorList, values);

        Console.ReadLine();
    }

    public static void AddDataTableColWithType(DataTable dtName, string[,] colNameAndType)
    {
        for (int i = 0; i < colNameAndType.Length / 2; i++)
        {
            dtName.Columns.Add(colNameAndType[i, 0], PrimitiveTypes[colNameAndType[i, 1]]);
        }
    }