Results 1 to 6 of 6

Thread: [RESOLVED] Inserting columns into data table, help please

  1. #1

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Resolved [RESOLVED] Inserting columns into data table, help please

    Howdy friends!

    I have been struggling with this for a while and dont understand why it isnt working. I am trying to add columns and rows into a preexisting datatable at certain indexes. I can do this quite well with rows using the following code:

    VB.Net Code:
    1. 'working code for adding rows
    2.                         Dim drNew As DataRow = dtCOPY.NewRow
    3.                         dtCOPY.Rows.InsertAt(drNew, r + A)

    ... however I cannot achieve the same results with columns. I am currently using this code:

    VB.Net Code:
    1. 'not working code for adding columns
    2.                         Dim addAT As Integer = c + A
    3.                         dtCOPY.Columns.Add("").SetOrdinal(addAT)

    ... but instead of creating a column at the index of 'addAT' it always creates a new column at the end of the data table. It even does the same thing when I place the value of the 'SetOrdinal' to 1. Please instruct me as to where I am going wrong because this is quite confusing .

    Many thanks,
    ~Frab
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Inserting columns into data table, help please

    I suspect that the code that you've shown is working exactly as it should and you're doing something wrong elsewhere. I just tested this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private table As New DataTable
    4.  
    5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    6.         With table
    7.             .Columns.Add("First", GetType(String))
    8.             .Columns.Add("Second", GetType(String))
    9.         End With
    10.  
    11.         DataGridView1.DataSource = table
    12.     End Sub
    13.  
    14.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    15.         With table
    16.             .Columns.Add("Third", GetType(String)).SetOrdinal(1)
    17.         End With
    18.  
    19.         DataGridView2.DataSource = table
    20.     End Sub
    21.  
    22. End Class
    and it worked exactly as expected. I clicked Button1 and I saw columns added to DataGridView1 with column headers reading "First" and "Second" in that order. I then clicked Button2 and I saw columns added to DataGridView2 with column headers reading "First", "Third" and "Second" in that order.

    I would suggest that you create a new project and test that minimal code that I did. If it works then there's obviously something wrong with your other project, either in your code or due to some corruption somewhere. If it doesn't work then it would seem that there's something wrong with your system and maybe VS needs repairing.

  3. #3

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: Inserting columns into data table, help please

    Quote Originally Posted by jmcilhinney View Post

    I would suggest that you create a new project and test that minimal code that I did. If it works then there's obviously something wrong with your other project, either in your code or due to some corruption somewhere. If it doesn't work then it would seem that there's something wrong with your system and maybe VS needs repairing.
    I did as you suggested and the new project did exactly the same thing. I copied your coded, placed two buttons and a datagridview inside the new form. Clicking button one resulted in creating columns "First" and "Second". Clicking button two resulted in "First", "Second" and then "Third". (please see image)

    *-* EDIT *-*

    I have just tried to repair my VS2013 from the control panel and the end result was the same.

    So what now?

    ~Frab

    Name:  Capturetest.jpg
Views: 884
Size:  15.8 KB
    Last edited by Frabulator; Jun 25th, 2017 at 09:29 AM.
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Inserting columns into data table, help please

    You didn't copy my code if you only used one DataGridView. I wrote, both in my code and my post, that I used TWO DataGridViews. If you're only using one then the code is working correctly and it is you doing the wrong thing to check whether it's working. Do as I showed and you will see that it's working. If you do it with one grid then, when you bind the second time, only the new column gets added to the grid and it will be added after all the existing columns but that is only in the grid. The order in the grid is not necessarily the same as the order in the DataTable. If you use two grids then it will be the same because you're starting with an empty grid instead of one that already has two columns in it.

  5. #5

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: Inserting columns into data table, help please

    Quote Originally Posted by jmcilhinney View Post
    You didn't copy my code if you only used one DataGridView. I wrote, both in my code and my post, that I used TWO DataGridViews. If you're only using one then the code is working correctly and it is you doing the wrong thing to check whether it's working. Do as I showed and you will see that it's working. If you do it with one grid then, when you bind the second time, only the new column gets added to the grid and it will be added after all the existing columns but that is only in the grid. The order in the grid is not necessarily the same as the order in the DataTable. If you use two grids then it will be the same because you're starting with an empty grid instead of one that already has two columns in it.
    You are correct, I did change your code to only use one datagridview, but I did that because I thought 'DataGridView2' was a typo

    Anyway, thank you. Your response led me to the solution. By setting the DataSource of the DGV to nothing before adding columns and then rebinding afterword I was able to add the column to the same DGV in the correct order. Please see my revised code for anyone that is interested.

    VB.NET Code:
    1. Private table As New DataTable
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         With table
    5.             .Columns.Add("First", GetType(String))
    6.             .Columns.Add("Second", GetType(String))
    7.         End With
    8.  
    9.         DataGridView1.DataSource = table
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    13.         DataGridView1.DataSource = Nothing
    14.         DataGridView1.Refresh()
    15.  
    16.         With table
    17.             .Columns.Add("Third", GetType(String)).SetOrdinal(1)
    18.         End With
    19.  
    20.         DataGridView1.DataSource = table
    21.     End Sub
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Inserting columns into data table, help please

    You said in the first place that the issue was that the column was not being added to the DataTable in the correct position but you never actually looked at the DataTable itself to find out. I used grids in my example because it was an easy visual way to demonstrate that the code worked but, if I was just doing it myself, I'd have used the debugger to simply look at the Columns collection of the DataTable itself and that's what you should have done. Hopefully you can see why, given that the code you thought was the issue was not and the actual issue was elsewhere. Had you gone to the source then there would have been no elsewhere.

Posting Permissions

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



Click Here to Expand Forum to Full Width