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();
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:
var table3 = table1.AsEnumerable()
.Where(row => table2.Rows.Find(row["ID"]) != null)
.CopyToDataTable();
Modify that as you need and then bind the result to your grid.