DataTable as subset of DataTable RRS feed

  • Question

  • I want to create a DataTable that is a subset of a DataTable that was created with a DataAdapter from SQLServerExpress. 


    If the Parent DataTable contains columns A ... K, I want my subset table to contain only columns A,B,C.  I am trying to avoid a second da.fill from the server with SELECT A,B,C & I don't gain anything vs. the second da.fill by doing a brute-force read-write. 


    I have looked at using the DataView.ToTable method, but I don't understand how to get only columns A,B,C into the dataview.


    The purpose of all of this is to give a datasource to a grid that contains only columns A,B,C so I don't have to go thru the hiding of all the unwanted columns (& maintain at least the fiction of being memory-usage-conscious). 


    Help would be appreciated!


    Don Seydel


    Saturday, July 28, 2007 9:54 PM

All replies

  • here what you have to do

    suppose T1 is your datatable with A,B,C,...,K columns

    create a new datatabe T2 as follows:

    DataTable T2 = new DataTable();

    now if you want to add to T2 the data from T1 and only columns A, and B then:

    foreach(DataRow dr in T1.Rows)


    DataRow dr2 = T2.NewRow();

    dr2Angel = drAngel.ToString();

    dr2Beer = drBeer.ToString();




    your grid grd.DataSource = T2;

    Hope this helps!!

    Sunday, July 29, 2007 11:09 AM
  • This is what I meant by the "brute-force" copying of the data.  I'm trying to avoid that for a number of reasons.


    I had also considered doing dtSubset = dtParent and then doing a column.remove(D...K), but that's not a very elegant solution.


    Thanks for your help!


    Don Seydel

    Sunday, July 29, 2007 4:42 PM