Results 1 to 2 of 2

Thread: How to get same Items Exist in two Datatables in third datatable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    How to get same Items Exist in two Datatables in third datatable

    Problem

    How to get same Items Exist in two Data tables in third data table by linq and display in datagridview

    in windows form application visual studio 2015.

    Meaning I Have two datatables

    First Data table is dt Get data from Excel .

    Second is dtItems get data from sql server 2014 database .

    I need when I have itemcode 12 and this item exist on two datatables

    dt(display data from excel) AND dtItems (Display data from sql server)

    show them in datagridview .


    if itemcode 12 exist on dt and dtItems display on datagridview .

    so that how to get similar it Items between two datatable by linq to sql in datagridview .

    Code:
    public DataTable ShowExcelData()
            {
                string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
    
                OleDbConnection con = new OleDbConnection(connectionString);
    
    
                con.Open();
                DataTable dt = new DataTable();
    
                dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    
                string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
    
    
                OleDbCommand com = new OleDbCommand();
                com.Connection = con;
               
                com.CommandText = @"SELECT  [ItemCode],[ItemsName],[ItemAddress] FROM  [" + SheetName + "] ";
                OleDbDataAdapter oledbda = new OleDbDataAdapter();
                oledbda.SelectCommand = com;
                DataSet ds = new DataSet();
                oledbda.Fill(ds);
                dt = ds.Tables[0];
                con.Close();
                return dt;
    
    
            }
    dt = ShowExcelData();
    
     public DataTable GetSqlItems()
            {
                string GetItems = @"select ItemCode,ItemsName,ItemAddress from Items";
    
    
               DataTable tbGetItems = DataAccess.ExecuteDataTable(GetItems );
                return tbGetItems ;
            }
    dtItems = GetSqlItems();

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get same Items Exist in two Datatables in third datatable

    This will create a new DataTable containing all the rows from 'table1' with an ID that is also present in 'table2':
    csharp Code:
    1. var table3 = table1.AsEnumerable()
    2.                    .Where(row => table2.Rows.Find(row["ID"]) != null)
    3.                    .CopyToDataTable();
    Modify that as you need and then bind the result to your grid.
    Last edited by jmcilhinney; Sep 24th, 2018 at 09:18 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width