Removing Columns From A DataView

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hey folks,

I have a Global DataSet which I use across my application. I use a DataView
to give me the data that I want in a certain instance.

Is there a way I can create the DataView so that it has only some of the
columns of the DataSet (i.e. not all of them)?

If not, can I reorder the columns? Like, can I specify that I want "myCol"
to be the second column?

Thanks!
 
John,

I assume you are talking about a datatable and not a dataset, because a
dataview is not related to a dataset.

A dataview is only a view on a datatable and holds only that plus the
sortorder and the rowfilter. So in my opinion would you have to use the
datatable for what you want.

Although I do not see the sense of reordering the columns, can you use a
remove, add, removeat unluckily not for you an insertat so you have to
rearange that using removeat and add them again yourself in my opinion.

http://msdn.microsoft.com/library/d...ystemdatadatacolumncollectionmemberstopic.asp

I hope this helps?

Cor
 
John,

Like Cor said you have to accomplish this utilizing the datatable. If you
want to remove the column(s) in the view you will have to remove them from
the datatable then assign the view to the datatable. If you want to keep the
table in the original format you can simply copy the datatable to another
datatable from which you derive the dataview. I have included a sample below
to demonstrate my point.

I hope this helps.
---------------------------
//place code in a win form project with
//2 datagrids that have their default names
//this is using the Northwind db

SqlConnection conn = new SqlConnection(strConn);
DataSet ds_orig = new DataSet();
DataSet ds_copy = new DataSet();
DataTable dt;
DataView dv;

SqlDataAdapter da = new SqlDataAdapter("SELECT Title,FirstName,LastName FROM
Employees",conn);
da.Fill(ds_orig);
ds_copy = ds_orig.Copy();
dt = ds_copy.Tables[0];
dt.Columns.Remove("Title");
dv = new DataView(dt);

dataGrid1.DataSource = ds_orig;
dataGrid2.DataSource = dv;
 
Thx Cor and Brian. That's what I ended up doing (copying the datatable and
removing the columns from there).
 
Back
Top