-
Jun 25th, 2017, 12:59 AM
#1
Thread Starter
Hyperactive Member
[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:
'working code for adding rows
Dim drNew As DataRow = dtCOPY.NewRow
dtCOPY.Rows.InsertAt(drNew, r + A)
... however I cannot achieve the same results with columns. I am currently using this code:
VB.Net Code:
'not working code for adding columns
Dim addAT As Integer = c + A
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
-
Jun 25th, 2017, 01:46 AM
#2
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:
Public Class Form1 Private table As New DataTable Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click With table .Columns.Add("First", GetType(String)) .Columns.Add("Second", GetType(String)) End With DataGridView1.DataSource = table End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click With table .Columns.Add("Third", GetType(String)).SetOrdinal(1) End With DataGridView2.DataSource = table End Sub 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.
-
Jun 25th, 2017, 09:01 AM
#3
Thread Starter
Hyperactive Member
Re: Inserting columns into data table, help please
Originally Posted by jmcilhinney
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
Last edited by Frabulator; Jun 25th, 2017 at 09:29 AM.
-
Jun 25th, 2017, 10:29 AM
#4
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.
-
Jun 25th, 2017, 12:20 PM
#5
Thread Starter
Hyperactive Member
Re: Inserting columns into data table, help please
Originally Posted by jmcilhinney
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:
Private table As New DataTable Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click With table .Columns.Add("First", GetType(String)) .Columns.Add("Second", GetType(String)) End With DataGridView1.DataSource = table End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click DataGridView1.DataSource = Nothing DataGridView1.Refresh() With table .Columns.Add("Third", GetType(String)).SetOrdinal(1) End With DataGridView1.DataSource = table End Sub
-
Jun 25th, 2017, 05:36 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|