Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding a new row to DataSource [modified]

Adding a new row to DataSource [modified]

Scheduled Pinned Locked Moved Visual Basic
tutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Floodlight
    wrote on last edited by
    #1

    Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:

    Dim Table1 As DataTable
    Table1 = New DataTable("Useful Links")
    '------------------------------------------------------------
    Dim adModel As DataColumn = New DataColumn("adModel")
    adModel.DataType = System.Type.GetType("System.String")
    Table1.Columns.Add(adModel)

        Dim unId As DataColumn = New DataColumn("unId")
        unId.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(unId)
        '------------------------------------------------------------
        Dim ds As New DataSet()
        ds.Tables.Add(Table1)
        dg.DataSource = Table1
        '------------------------------------------------------------
    

    There will be a button (or something) to add rows to that DataSource.

    modified on Wednesday, March 17, 2010 9:14 PM

    L D 2 Replies Last reply
    0
    • F Floodlight

      Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:

      Dim Table1 As DataTable
      Table1 = New DataTable("Useful Links")
      '------------------------------------------------------------
      Dim adModel As DataColumn = New DataColumn("adModel")
      adModel.DataType = System.Type.GetType("System.String")
      Table1.Columns.Add(adModel)

          Dim unId As DataColumn = New DataColumn("unId")
          unId.DataType = System.Type.GetType("System.String")
          Table1.Columns.Add(unId)
          '------------------------------------------------------------
          Dim ds As New DataSet()
          ds.Tables.Add(Table1)
          dg.DataSource = Table1
          '------------------------------------------------------------
      

      There will be a button (or something) to add rows to that DataSource.

      modified on Wednesday, March 17, 2010 9:14 PM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Check it . http://msdn.microsoft.com/en-us/library/5ycd1034(VS.80).aspx[^]

      If you can think then I Can.

      F 1 Reply Last reply
      0
      • L Lost User

        Check it . http://msdn.microsoft.com/en-us/library/5ycd1034(VS.80).aspx[^]

        If you can think then I Can.

        F Offline
        F Offline
        Floodlight
        wrote on last edited by
        #3

        Thanks for the response but this doesn't appear to be what I'm after... I am no expert (particularly with datatables) but the method you suggested appears to require a DataSet. Like mentioned earlier, my data is created at runtime (called Table1) on form.load so I am unsure how to refer to it programmatically... Forgive me if I am not making sense, or going the wrong way about all this, as previously mentioned I am not disciplined in datatables! :sigh:

        L 1 Reply Last reply
        0
        • F Floodlight

          Thanks for the response but this doesn't appear to be what I'm after... I am no expert (particularly with datatables) but the method you suggested appears to require a DataSet. Like mentioned earlier, my data is created at runtime (called Table1) on form.load so I am unsure how to refer to it programmatically... Forgive me if I am not making sense, or going the wrong way about all this, as previously mentioned I am not disciplined in datatables! :sigh:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          it is easy use like this Dim dsObj As CRDataSet = New CRDataSet ' Your DataSource Here dsObj.tablename.addmaintablerow(str1,str2,str3)

          If you can think then I Can.

          1 Reply Last reply
          0
          • F Floodlight

            Hello all, I have been pulling my hair our trying to get this to work. I created a DataSource programatically and applied it to a DataGridView. Now, I want to add rows to that DataSource programatically and have it displayed in the DataGridView, but I can't for the life of me work out how to add to the DataSource.... I know there must be a relatively simple way to do this.. I can't add data to it in the usual way because the DataGridView is bound. Any advice is much appreciated! =) Here's the code for creating the DataSource and adding columns to it on form.load, and showing it in my DataGridView (named "dg"). This part of the code works fine:

            Dim Table1 As DataTable
            Table1 = New DataTable("Useful Links")
            '------------------------------------------------------------
            Dim adModel As DataColumn = New DataColumn("adModel")
            adModel.DataType = System.Type.GetType("System.String")
            Table1.Columns.Add(adModel)

                Dim unId As DataColumn = New DataColumn("unId")
                unId.DataType = System.Type.GetType("System.String")
                Table1.Columns.Add(unId)
                '------------------------------------------------------------
                Dim ds As New DataSet()
                ds.Tables.Add(Table1)
                dg.DataSource = Table1
                '------------------------------------------------------------
            

            There will be a button (or something) to add rows to that DataSource.

            modified on Wednesday, March 17, 2010 9:14 PM

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            If you're just binding the DGV to a single table, with no relations to other tables, you don't even need the DataSet. A DataSet is just a collection of DataTable object and DataRelations between them. Since your DGV is bound to a DataTable, you just need to add a new row to the DataTable, not to the DGV. Look into the DataTable.NewRow method. An alternative to doing it manually would be to create a BindingSource object, set it's DataSource to your DataTable, then set the DGV's DataSource to the BindingSource you created. All navigation and row handling is handled by the BindingSource object.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            F 1 Reply Last reply
            0
            • D Dave Kreskowiak

              If you're just binding the DGV to a single table, with no relations to other tables, you don't even need the DataSet. A DataSet is just a collection of DataTable object and DataRelations between them. Since your DGV is bound to a DataTable, you just need to add a new row to the DataTable, not to the DGV. Look into the DataTable.NewRow method. An alternative to doing it manually would be to create a BindingSource object, set it's DataSource to your DataTable, then set the DGV's DataSource to the BindingSource you created. All navigation and row handling is handled by the BindingSource object.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              F Offline
              F Offline
              Floodlight
              wrote on last edited by
              #6

              Thanks for the explanation Dave, that helped alot. Gonna try a few of your suggestions now and see how I go :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups