all groups > vj# > december 2004 >
You're in the

vj#

group:

j# DataTable add columns?


j# DataTable add columns? phodge
12/10/2004 7:43:01 PM
vj#: Do you know of a way to add columns to a DataTable? I have to use a
DataView, which needs a DataTable, with columns.
Re: j# DataTable add columns? Lars-Inge Tønnessen [VJ# MVP]
12/11/2004 10:45:18 PM
[quoted text, click to view]

Yes, we do.


Lets say you have this datatable.
System.Data.DataTable dt = new System.Data.DataTable("One Table");


( fill some data into the data table)
sqlDataAdapter1.Fill( dt );


Make a dataview to hold the data table.
System.Data.DataView dv = new System.Data.DataView( dt );


Add a new column to the data tabel and the dataview.
dt.get_Columns().Add("A new column");


Or you can do this:
dv.get_Table().get_Columns().Add("A second column");



You can view the DataView through a DataGrid WinForms GUI control with this
code:

dataGrid1.set_DataSource( dv );



Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button