Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 2-Nov-12 20:38pm
devausha1.4K
Edited 2-Nov-12 20:39pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may use this code for your task.
 
DataTable dtTarget = new DataTable();
    dtTarget = dtSource.Clone();
    DataRow[] rowsToCopy;
    rowsToCopy = dtSource.Select("key='" + matchString + "'");
    foreach (DataRow temp in rowsToCopy)
    {
        dtTarget.ImportRow(temp);
    }
  Permalink  
v2
Comments
Sunnykumar08 at 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 at 3-Nov-12 5:52am
   
It works thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi devusha,
 
Well, in this case you add a column manually like:
 
dt1.Columns.Add("newColumnName",Type.GetType("String"));
 
and If you want to copy this column to another datatable, you can do like this:
 
DataTable dt2 = new DataTable();
dt2.Columns.Add(dt1.Columns["newColumnName"]);
 
Hope this helps!
 

Happy CodingSmile | :)
 
Best Regards,
Sunny K
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi devausha, What dont you look for joins?
I think it may help you
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 428
1 Sergey Alexandrovich Kryukov 283
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100