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. datatable.columns.add(datacolumn, index)

datatable.columns.add(datacolumn, index)

Scheduled Pinned Locked Moved Visual Basic
databasetutorialquestion
6 Posts 2 Posters 38 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.
  • J Offline
    J Offline
    jcrussell
    wrote on last edited by
    #1

    I have a datatable that I have retrieved from a database (w00t!) now I am trying to add a column to this datatable, but the add command only allows me to add a datacolumn to the end of the datatable collection :| I want to add my new datacolumn to the start of the datatable (or potentially at any index :~ ) and I haven't been able to figure out how to do this. Any hints? Thanks in Advance,

    Jason

    D 1 Reply Last reply
    0
    • J jcrussell

      I have a datatable that I have retrieved from a database (w00t!) now I am trying to add a column to this datatable, but the add command only allows me to add a datacolumn to the end of the datatable collection :| I want to add my new datacolumn to the start of the datatable (or potentially at any index :~ ) and I haven't been able to figure out how to do this. Any hints? Thanks in Advance,

      Jason

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

      You can't add a column at any index you want. Anything you add will always be appended to the end. Besides, a DataTable object has no visible component, so it doesn't matter where the column is appended... Why do you want to do this?

      Dave Kreskowiak Microsoft MVP - Visual Basic

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You can't add a column at any index you want. Anything you add will always be appended to the end. Besides, a DataTable object has no visible component, so it doesn't matter where the column is appended... Why do you want to do this?

        Dave Kreskowiak Microsoft MVP - Visual Basic

        J Offline
        J Offline
        jcrussell
        wrote on last edited by
        #3

        I'm setting it as my datagrid datasource. I'm reading data from several sources in my database. Based on the type of data I'm reading (whether it is Contact data or Business data etc) I wanted to add an icon column to my datagrid. This works fine, except the icon is the last column in my datagrid, I want it to be the first. I assumed that I would do this at the datatable level, not at the datagrid level.

        Jason

        D 1 Reply Last reply
        0
        • J jcrussell

          I'm setting it as my datagrid datasource. I'm reading data from several sources in my database. Based on the type of data I'm reading (whether it is Contact data or Business data etc) I wanted to add an icon column to my datagrid. This works fine, except the icon is the last column in my datagrid, I want it to be the first. I assumed that I would do this at the datatable level, not at the datagrid level.

          Jason

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

          The view of the data is maintained seperate from the data itself. The order of the columns in the datatable has nothing to do with the order of the columns in the datagrid. In order to do this, you'll have to create all your columns, in the order you want, and add them to the grid.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          J 1 Reply Last reply
          0
          • D Dave Kreskowiak

            The view of the data is maintained seperate from the data itself. The order of the columns in the datatable has nothing to do with the order of the columns in the datagrid. In order to do this, you'll have to create all your columns, in the order you want, and add them to the grid.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            J Offline
            J Offline
            jcrussell
            wrote on last edited by
            #5

            so how is this done? I already have my datatable (tblMergedContactsAndCompanies). I want to add a new column at index 0 and then read through all the rows in my datatable and assign an icon depending on Type (Contact or Company) and then display this in a datagrid. I can add the icon column to my datatable, but when I display it in my datagrid, it is at index(datatable.columns.count).

            Dave Kreskowiak wrote:

            In order to do this, you'll have to create all your columns, in the order you want, and add them to the grid.

            Are you saying that I have to create a new datatable, read the columns from my original datatable in, and then read the data in? :doh:

            Jason

            D 1 Reply Last reply
            0
            • J jcrussell

              so how is this done? I already have my datatable (tblMergedContactsAndCompanies). I want to add a new column at index 0 and then read through all the rows in my datatable and assign an icon depending on Type (Contact or Company) and then display this in a datagrid. I can add the icon column to my datatable, but when I display it in my datagrid, it is at index(datatable.columns.count).

              Dave Kreskowiak wrote:

              In order to do this, you'll have to create all your columns, in the order you want, and add them to the grid.

              Are you saying that I have to create a new datatable, read the columns from my original datatable in, and then read the data in? :doh:

              Jason

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

              No! You create the DataGrid columns manually and specify which columns they map to in the data source.

              Dave Kreskowiak Microsoft MVP - Visual Basic

              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