Results 1 to 4 of 4

Thread: Problems adding a datacolumn to a datatable

  1. #1
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,198

    Problems adding a datacolumn to a datatable

    I'm trying to dynamically add datacolumns to a datatable, then reflect those changes in a datagridview. This is what I have to add them:
    Code:
    Private Sub BindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
            Dim i As Integer = DataSet1.Tables(0).Columns.Count + 1
            Dim col As New DataColumn
            col.ColumnName = i.ToString
    
            'Creates a blank row if there are no columns
            If i = 1 Then
                DataSet1.Tables(0).NewRow()
            End If
    
            'Creates the new column
            DataSet1.Tables(0).Columns.Add(col)
    
            'Refreshes the datagridview
            DataGridView1.Refresh()
        End Sub
    This is what I'm using to remove them:
    Code:
    Private Sub BindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
            'Iterates through each selected column
            For Each col As DataGridViewColumn In DataGridView1.SelectedColumns
                'Removes those columns
                DataSet1.Tables(0).Columns.Remove(col.Name)
            Next
    
            'If there are no columns left then delete all the rows
            If DataSet1.Tables(0).Columns.Count = 0 Then
                For Each dr As DataRow In DataGridView1.Rows
                    DataSet1.Tables(0).Rows.Remove(dr)
                Next
            End If
    
            'Refreshes the datagridview
            DataGridView1.Refresh()
        End Sub
    When I click my add button, the countitem goes up and when I click my delete button the countitme goes down. My problem is that the changes aren't reflecting in the datagridview. The dataset is untyped. The bindingsource and the datagridview's datasource is dataset1 and the datamember is table1. Any suggestions?

  2. #2
    Frenzied Member
    Join Date
    Aug 09
    Location
    Los Angeles
    Posts
    1,308

    Re: Problems adding a datacolumn to a datatable

    Shouldnt "DataSet1.Tables(0)"
    be
    "DataSet1.Tables(1)
    or
    DataSet1.Tables("Tables1")

    ?

  3. #3
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,007

    Re: Problems adding a datacolumn to a datatable

    The following loads a DataTable into a BindingSource, the BindingSource.DataSource is assigned the DataTable
    Code:
    Public Class Form1
        WithEvents bsData As New BindingSource
    Code:
    bsData.DataSource = LoadMockedServiceItems()
    The DataGridView DataSource is the BindingSource
    Code:
    DataGridView1.DataSource = bsData
    Pressing a Button either adds or removes a specific column from the DataTable as per above. When this happens the DataGridView reflects the add or removal of the column NewColumn1.
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = DirectCast(bsData.DataSource, DataTable)
        If dt.Columns.Contains("NewColumn1") Then
            dt.Columns.Remove("NewColumn1")
        Else
            dt.Columns.Add(New DataColumn With {.ColumnName = "NewColumn1"})
        End If
    End Sub

  4. #4
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,198

    Re: Problems adding a datacolumn to a datatable

    Shouldnt "DataSet1.Tables(0)"
    be
    "DataSet1.Tables(1)
    Nope, it could be Table1 because that's the name of it. However, an index out of range would get thrown if I change it to 1.

    @Kevin,
    Thank you for the detailed reply. I get it now :]

Posting Permissions

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