tags:

views:

104

answers:

1

Using the code below (from a console app I've cobbled together), I add seven columns to my datatable. Once this is done, how can I set the data type for each column? For instance, column 1 of the datatable will have the header "ItemNum" and I want to set it to be an Int. I've looked at some examples on thet 'net, but most all of them show creating the column header and column data type at once, like this:

loadDT.Columns.Add("ItemNum", typeof(Int));

At this point in my program, the column already has a name. I just want to do something like this (not actual code):

loadDT.Column[1].ChangeType(typeof(int));

Here's my code so far (that gives the columns their name):

// get column headings for datatable by reading first line of csv file.
        StreamReader sr = new StreamReader(@"c:\load_forecast.csv");
        headers = sr.ReadLine().Split(','); 
        foreach (string header in headers)
        {
            loadDT.Columns.Add(header);
        }     

Obviously, I'm pretty new at this, but trying very hard to learn. Can someone point me in the right direction? Thanks!

+1  A: 

You should be able to assign the column's data type property so long as there is no data stored in that column yet:

loadDT.Column[1].DataType = typeof(int);
Michael Petito
Yep, at this point in the program, there is no data in the datatable. Awesome! Thank you so much for the quick response! I greatly appreciate it. C# is getting more fun every day!
Kevin