Search This Blog

Saturday, March 3, 2012

How to Compare The Two data table Columns in C sharp

Q)I have one issue , my issue is
1)i want to compare the two data table columns in C sharp
2) if columns in first datatable matches with columns in second datatable
3) i want to copy those matched column data to another datatable

please help me how to do it.
By using  your code we can compare but  if the column matches with other i want to copy entire column data to another datatable .
eg;
datatable 1                                                  datatable 2
col1   col2   col3                                          col1     col2
1       test     mumbai                                       1       test
2       test     london                                         2      cool
3       test2   america                                        3       testing


i want to matched column data to another datatable
datatable 3
col1   col2
1        test
2
3


Answer)

DataTable datatable1=null; //Your Datatable1
DataTable datatable2=null; //Your Datatable2
DataTable dtMatchData = new DataTable("dtMatchData");
        dtMatchData = datatable1.Copy();
        foreach (DataColumn dt1col in datatable1.Columns)
        {
            if (datatable1.Columns.Contains(Convert.ToString(datatable2.Columns[dt1col.ColumnName])) == false)
            {
                dtMatchData.Columns.Remove(dt1col.ColumnName);

            }
        }


 

11 comments:

  1. I do not drop a comment, but I looked at a few of the remarks on
    "How to Compare The Two data table Columns in C sharp". I actually do
    have a few questions for you if you tend not to mind. Is it simply me or does it seem like some of these responses look
    as if they are coming from brain dead individuals? :-P And, if you are posting at additional sites, I would
    like to keep up with you. Would you post a list of all of your social sites like your Facebook page, twitter feed, or linkedin profile?


    Also visit my blog Diet Patch Review

    ReplyDelete
  2. Woah! I'm really digging the template/theme of this site. It's simple,
    yet effective. A lot of times it's challenging to get that "perfect balance" between usability and appearance. I must say you've
    done a awesome job with this. Also, the blog loads
    super quick for me on Opera. Excellent Blog!

    my webpage; Green coffee reviews

    ReplyDelete
  3. Spot on with this write-up, I actually believe that this website needs far more attention.
    I'll probably be back again to read more, thanks for the information!

    My blog post ... Max Thermo Burn Review

    ReplyDelete
  4. Very energetic blog, I liked that a lot. Will there be a part
    2?

    Also visit my web site: Automated pay days reviews

    ReplyDelete
  5. Heya i am for the first time here. I came across this board and I find It really useful &
    it helped me out a lot. I hope to give something back and help others like
    you helped me.

    Check out my website - Dermal meds facts

    ReplyDelete
  6. Thanks for another wonderful post. The place else may just anybody get that type of
    info in such a perfect approach of writing? I have a presentation
    subsequent week, and I'm on the search for such info.

    my webpage: Rvtl

    ReplyDelete
  7. Very shortly this web site will be famous amid all blogging and site-building people,
    due to it's good posts

    Feel free to surf to my web blog - Pur Essence Anti Aging

    ReplyDelete
  8. Hello There. I found your blog the usage of msn. That is an extremely well written article.
    I will be sure to bookmark it and come back to learn more of your helpful info.

    Thank you for the post. I'll certainly comeback.

    Here is my webpage ... Ultra Slim patch

    ReplyDelete
  9. Hey there! I just would like to offer you a huge thumbs up
    for the excellent info you've got here on this post. I am returning to your site for more soon.

    my page - acai ultra lean reviews

    ReplyDelete
  10. Great blog! Do you have any tips and hints for aspiring writers?
    I'm planning to start my own blog soon but I'm a little lost on
    everything. Would you recommend starting with a free platform like Wordpress or go
    for a paid option? There are so many options out there that I'm totally overwhelmed .. Any recommendations? Many thanks!

    my web-site ... green coffee reviews

    ReplyDelete
  11. Helpful information. Lucky me I found your web site unintentionally, and I am stunned why this twist of fate did not took place in advance!
    I bookmarked it.

    Also visit my blog post: Order Goji Berry

    ReplyDelete