CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    28

    [RESOLVED] Datatable - creating new columns ignores datatype?

    I have a datatable which has a number of rows containing data

    Then I need to add some empty datacolumns which I do like this:

    Code:
       
    Dim OT_Hours As New DataColumn("OT_Hours")
    OT_Hours.DataType = System.Type.GetType("System.Double")
    OT_Hours.DefaultValue = 0
    MyTable.Columns.Add("OT_Hours")
    When I come to use the datatable I find that these new columns now have datatype String and initial values of ""

    I tested this using:

    Code:
    For Each column As DataColumn In MyTable.Columns
    
      Console.WriteLine(column.ColumnName & " is a " & Type.GetTypeCode(column.DataType).ToString) 
    
    Next
    I need to use these columns for calculations

    any ideas?

    What am I doing wrong?
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

  2. #2
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Datatable - creating new columns ignores datatype?

    When you add "OT_Hours" you are creating a new column with that name not using the OT_Hours you created earlier. You need to:

    MyTable.Columns.Add(OT_Hours)

  3. #3
    Join Date
    Oct 2006
    Posts
    28

    Smile Re: Datatable - creating new columns ignores datatype?

    Thank you so much, problem solved
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured