Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am stored values to datatable from database.
My datatable has following
jc_id  jc_name jc_car        jc_color  jc_plate jc_service  jc_spare jc_qty
1	asdfsa	Maruti800	red	123	NULL	    NULL	NULL
NULL	NULL	NULL		NULL	NULL	WaterWash   NULL	NULL
NULL    NULL    NULL            NULL    NULL    WaterWash   NULL         NULL
NULL    NULL    NULL            NULL    NULL    NULL        Star Wars   34.00
NULL    NULL    NULL            NULL    NULL    NULL        Shrek   43.00



In this datatable I want a jc_service column without null value
like
jc_service
WaterWash
WaterWash


I want to this copy this column only to another datatable how to do it?
Please help me...
Posted
Updated 2-Nov-12 20:39pm
v2

You may use this code for your task.

C#
DataTable dtTarget = new DataTable();
    dtTarget = dtSource.Clone();
    DataRow[] rowsToCopy;
    rowsToCopy = dtSource.Select("key='" + matchString + "'");
    foreach (DataRow temp in rowsToCopy)
    {
        dtTarget.ImportRow(temp);
    }
 
Share this answer
 
v2
Comments
Sunny_Kumar_ 3-Nov-12 3:34am    
Hi subhajit, We must read the questions carefully before answering them as you can see, the question is about to copy a column to another datatable instead of cloning a whole table. Hope you get my point :)
devausha 3-Nov-12 5:52am    
It works thank you
Hi devusha,

Well, in this case you add a column manually like:

C#
dt1.Columns.Add("newColumnName",Type.GetType("String"));


and If you want to copy this column to another datatable, you can do like this:

C#
DataTable dt2 = new DataTable();
dt2.Columns.Add(dt1.Columns["newColumnName"]);


Hope this helps!


Happy Coding:)

Best Regards,
Sunny K
 
Share this answer
 
Hi devausha, What dont you look for joins?
I think it may help you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900