none
How to Programically add a DataColumn to a Typed DataSet RRS feed

  • Question

  • I've been using the DataSet wizards to create Dataset of my projects database tables.  One of my tables has dynamic columns (i.e. My Items table tracks the number of Inventory Items at each location).  The end user might add an Inventory Location such as "Truck3" or "Wharehouse4" in which case I would add a corresponding column to the Items table that stores the number of Inventory items stored at the given location.  So how do I add rows to the ItemsDataSet dynamically? It seems that I would need/want to use expression columns but do I need to add 50 expression columns to the dataset’s designer now and set their names dynamically (if that is possible).

    This is what I have so far…,

    Private Sub AddColumnsToItemsDataSet()

            'GET THE ROWS IN THE THE INVENTORY PLACES TABLE (i.e. Wharehouse1,2,3 etc.)

                  Dim DS As New InventoryPlacesDataSet

    Dim InventoryPlacesTA As New       InventoryPlacesDataSetTableAdapters.InventoryPlacesTableAdapter()

                  Dim InvLoc As Integer = InventoryPlacesTA.Fill(DS.InventoryPlaces)

                  Dim row As InventoryPlacesDataSet.InventoryPlacesRow

     

            For Each row In DS.InventoryPlaces.Rows

     

                If Not (row.IsNameNull) Then

                    If Not (row.Name = String.Empty) Then

     

                      ‘Add THESE ROWS AS COLUMNS TO THE ITEMSDATASET

                        Dim oTruck1 As New DataColumn

                        Dim t As ItemsDataSet.ItemsDataTable

     

                        With oTruck1

                            .DataType = System.Type.GetType("System.Double", False, False)

                            Dim INamesString As String

                            INamesString = row.Name

                            .ColumnName = INamesString

                            .DefaultValue = System.String.Empty

                        End With

     

                        With t.Columns

                            .Add(oTruck1)

                        End With

     

                        Dim r As DataRow

                        r = t.NewRow

                        t.Rows.Add(r)

                        Dim dView As New DataView

                        dView.Table = t

                        'DataGrid1.DataSource = dView

                    End If

                End If

            Next

    End Sub

     

    I’ve replaced Dim t As DataTable with Dim t As ItemsDataSet.ItemsDataTable.   Somehow I doubt that that works.  

    Tuesday, June 15, 2010 11:06 PM

Answers

  • It looks like you're on the right track.  As your code shows, you can add columns and rows to a DataSet using Columns.Add and Rows.Add.  If you're looking to dynamically add columns based on user input, TableAdapters may not prove to be terribly helpful.

    If you could provide a little more information about the scenario, I'd be happy to try to provide some guidance.  How and when are the additional columns being added to the database?  Will your application need to simply retrieve that data, or will it also need to be able to submit changes?


    David Sceppa
    Wednesday, June 16, 2010 5:37 AM
    Moderator