How can I add the column data type after adding the column headers to my datatable?

Posted by Kevin on Stack Overflow See other posts from Stack Overflow or by Kevin
Published on 2010-04-02T01:41:46Z Indexed on 2010/04/02 1:43 UTC
Read the original article Hit count: 297

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about datatable