Search Amazon:

How do I add new columns to a .NET DataTable?

This code shows how to add new columns to a datatable. It uses 2 datatables, one contains Sales Person data while the second contains Sales figures. Each sales person can have many sales. It is desired to add details for the top sale for each sales person to the Sales Person datatable in the form of new columns.

The VB.NET code below shows how to add new DataColumns to a DataTable.

Retrieve data from the database:

    Imports System.Data.OleDb
    '
    ' Retrieve the data.
    '
    Dim Connection As New OleDbConnection(strConnection)
    Dim DS As New DataSet

    Dim strSQL As String = _
          "Select * From SalesAssociates Order By SalesPersonId"
    Dim DA As New OleDbDataAdapter(strSQL, Connection)
    DA.Fill(DS, "SalesPerson")

    strSQL = "Select SalesPersonID,SaleAmount,OrderNum," & _
             "ItemName From Sales Order By SaleAmount Desc"
    DA = New OleDbDataAdapter(strSQL, Connection)
    DA.Fill(DS, "SalesData")

Add the new columns to the SalesPerson datatable:

    Dim adc1 As DataColumn
    Dim adc2 As DataColumn
    Dim adc3 As DataColumn

    adc1 = New DataColumn("SaleAmount", System.Type.GetType("System.Decimal"))
    adc2 = New DataColumn("OrderNum", System.Type.GetType("System.Int32"))
    adc3 = New DataColumn("ItemName", System.Type.GetType("System.String"))

    DS.Tables("SalesPerson").Columns.Add(adc1)
    DS.Tables("SalesPerson").Columns.Add(adc2)
    DS.Tables("SalesPerson").Columns.Add(adc3)

Fill the new SalesPerson columns with the SalesData data:

    Dim aDR As DataRow
    Dim intResult As Integer
    '
    ' Process each sales person row.
    '
    For Each aDR In DS.Tables("SalesPerson").Rows
        '
        ' Filter the SalesData datatable by the current sales person.
        '
        DS.Tables("SalesData").DefaultView.RowFilter = _
              "SalesPersonID=" & aDR(SalesPersonID).ToString
        '
        ' If there is a sales row for this sales person populate the data.
        '
        intResult = DS.Tables("SalesData").DefaultView.Count - 1

        If intResult > 0 Then
            aDR("SaleAmount") = _
              DS.Tables("SalesData").DefaultView(0)("SaleAmount")
            aDR("OrderNum") = _
              DS.Tables("SalesData").DefaultView(0)("OrderNum")
            aDR("ItemName") = _
              DS.Tables("SalesData").DefaultView(0)("ItemName")
        End If
        '
        ' Remove the filter.
        '
       DS.Tables("SalesPerson").DefaultView.RowFilter = ""
    Next

    DS.Tables("SalesPerson").AcceptChanges()
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms