datatable.columns.add(datacolumn, index)
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
No! You create the DataGrid columns manually and specify which columns they map to in the data source.
Dave Kreskowiak Microsoft MVP - Visual Basic