-2

I have 2 Datatables that are quite similar but have other columnnames. The first Datatable has 4 columns and about 7000 records. The second one has also 4 columns and about 37000 records. Now i want to Display the records that are missing in the first Datatable in a third datagridview. How should I do this? I know there are a lot of code in the Internet about this but nothing has worked.

using Microsoft.Win32; 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;




namespace Compare
 {
  public partial class Form1 : Form
   {
    public Form1()
    {
        InitializeComponent();
    }


    DataTable ds;
    DataTable dt;

    public void btn_Click(object sender, EventArgs e)
    {
        SqlDataAdapter adapter = new SqlDataAdapter("select a.FALLZAHL, m.CODE, m.ANZ, m.STDAT From test m with (nolock) inner join test2 a with (nolock) on a.id = m.aid where m.STDAT >= '2016-01-01' and m.STALT = '6363' order by a.FALLZAHL", "Server = ada; Database = sd;Trusted_Connection = True");
        ds = new DataTable(" ");
        adapter.Fill(ds);
        dataGridView1.DataSource = ds;



    }

    private void Excelsheet_Click(object sender, EventArgs e)
    {

        openFileDialog1.ShowDialog();




    }

    private void choose_Click(object sender, EventArgs e)
    {
        OpenFileDialog OpenFileDialog = new OpenFileDialog();



    }
    private string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";

    private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={0}'";







    public void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
    {
        string filePath = openFileDialog1.FileName;

        string extension = Path.GetExtension(filePath);

        string header = radioButton1.Checked ? "YES" : "NO";

        string conStr, sheetName, cells;




        conStr = string.Empty;

        switch (extension)
        {



            case ".xls": //Excel 97-03

                conStr = string.Format(Excel03ConString, filePath, header);

                break;



            case ".xlsx": //Excel 07

                conStr = string.Format(Excel07ConString, filePath, header);

                break;
        }




        using (OleDbConnection con = new OleDbConnection(conStr))
        {

            using (OleDbCommand cmd = new OleDbCommand())
            {

                cmd.Connection = con;
                con.Open();

                System.Data.DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();




                con.Close();







            }

        }

        using (OleDbConnection con = new OleDbConnection(conStr))
        {

            using (OleDbCommand cmd = new OleDbCommand())
            {

                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {

                    dt = new DataTable();

                    cmd.CommandText = "SELECT * From [" + sheetName + "]";

                    cmd.Connection = con;

                    con.Open();

                    oda.SelectCommand = cmd;

                    oda.Fill(dt);

                    con.Close();


                    dataGridView2.DataSource = dt; 


                }

            }

        }
    }



   private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {


    }


    private void Form1_Load(object sender, EventArgs e)
    {
        /* dataGridView3.DataSource = from table1 in ds.AsEnumerable()
                      join table2 in dt.AsEnumerable() on table1.Field<int>("ColumnA") equals table2.Field<int>("ColumnA")
                      where table1.Field<int>("ColumnB") == table2.Field<int>("ColumnB") || table1.Field<string>("ColumnC") == table2.Field<string>("ColumnC") || table1.Field<object>("ColumnD") == table2.Field<object>("ColumnD")
                      select table1;
         dataGridView3.DataSource = from table1 in ds.AsEnumerable()
                      where !dataGridView3.Contains(table1)
                      select table1;
         */
    }

    public void CompareData()
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        var dsRowCount = ds.AsEnumerable().Count();
        var dtRowCount = dt.AsEnumerable().Count();

        if (dsRowCount > dtRowCount)
        {
            //Set main table to be dt as this has the least number of rows.
            dataGridView3.DataSource = NoMatches(dt, ds);
        }
        else
        {
            //Set main table to ds as this has the least number of rows OR tables have the same number of rows.
            dataGridView3.DataSource = NoMatches(ds, dt);
        }





         }

    private IEnumerable<DataRow> NoMatches(DataTable MainTable, DataTable SecondaryTable)
    {
        var matched = from table1 in MainTable.AsEnumerable()
                      join table2 in SecondaryTable.AsEnumerable()
                      on table1.Field<string>("ISH_FALLZAHL") equals table2.Field<string>("FAL")
                      where (table1.Field<string>("ML_CODE").Equals(table2.Field<string>("LST")))
                     || Convert.ToInt32(table1.Field<Int16>("ML_ANZ")) == Convert.ToInt32(table2.Field<double>("ST"))
                      select table1;

        return MainTable.AsEnumerable().Except(matched);
    }







       // dataGridView3.DataSource = CompareTables();


    }

    /*        public bool DatatablesAreSame()
            {



                if (ds.Rows.Count == dt.Rows.Count)

                    return true;

                foreach (DataColumn dc in ds.Columns)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (ds.Rows[i][dc.ColumnName = "ISH_FALLZAHL"] != dt.Rows[i][dc.ColumnName = "FAL"])
                        {


                        }
                    }
                }

                return true;
            }

       */

/*
    private void CompareTables()
    {

        try
        {



            var dt1Records = ds.AsEnumerable().Select(e1 => new { Id = e1["ISH_FALLZAHL"].ToString(), Name = e1["FAL"].ToString() });

            var dt2Records = dt.AsEnumerable().Select(e2 => new { Id = e2["ISH_FALLZAHL"].ToString(), Name = e2["FAL"].ToString() });



            var extraRecords = dt1Records.Except(dt2Records);




            dataGridView3.DataSource = extraRecords;




        }

        catch (Exception ex)

        { }

    }

  */ 





}
myworld
  • 23
  • 2
  • 10
  • 2
    Show your work first. – uTeisT Aug 09 '16 at 09:05
  • So if I gave you "a,b,c,d,e,f,g,h,i,j,k,l,m" and "a,e,i" how would YOU as a human work out what was missing? as a database, do they have UIDs? are the same UIDs used in both tables for the same line? are they litterally identical but missing parts, or similar – BugFinder Aug 09 '16 at 09:06
  • 2
    Possible duplicate of [Compare two DataTables and select the rows that are not present in second table](http://stackoverflow.com/questions/15713243/compare-two-datatables-and-select-the-rows-that-are-not-present-in-second-table) – Liam Aug 09 '16 at 09:07
  • Look at Left out join at msdn : https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – jdweng Aug 09 '16 at 09:07
  • Sorry i am not allowed to post the whole code but i can post that how i tried to Display the missing data when you Need it? – myworld Aug 09 '16 at 09:07
  • 1
    This question is asked roughly every 37 seconds. Try google next time – Liam Aug 09 '16 at 09:08
  • I have written that the code that i found in the Internet do not work for me !!! I work for 4 hours about this! – myworld Aug 09 '16 at 09:09
  • Then show your code atleast. – Abhay Dixit Aug 09 '16 at 09:14
  • Show your code and replace any sensitive data / code with example stuff. That will at least give us a chance to see how you're currently trying to do this. – sr28 Aug 09 '16 at 09:17
  • Can you describe what results you are getting rather than "nothing worked"? Also, can you show what should be happening with some example data? – sr28 Aug 09 '16 at 09:21
  • I get really nothing when i click at the bottom nothing happen. I have also set a breakpoint and sometime he Returns null – myworld Aug 09 '16 at 09:24
  • i added some example data in the question. – myworld Aug 09 '16 at 09:57
  • Nobody want to help me? – myworld Aug 09 '16 at 19:47
  • Have you checked that 'matched' and 'missing' are returning the values you would expect? – sr28 Aug 11 '16 at 08:21
  • they return null. I think that the code i use do not match with the tables because they do not have the same structure but the data is ident. I do not know what to do. I will post my whole code. – myworld Aug 11 '16 at 08:47
  • In your query for 'matched' why are you doing 'from table1 in ds.AsEnumerable()'? If you are trying to compare a table within the dataset to another in the dataset you will need to reference the table in the dataset, not just the dataset. – sr28 Aug 11 '16 at 09:05
  • yes i know i have improved it, but the code is still worong i Need a code who compare to tables with not exactly the same shema anyone know where to find it? – myworld Aug 11 '16 at 09:12

1 Answers1

1

To compare 2 data tables within a data set you will need to actually reference those tables. Looking at your code it looks like you're trying to join a data table that's been filled from a spreadsheet to a whole dataset, which won't work.

Try something like this:

var matched = from table1 in ds.Tables["Your Table"].AsEnumerable()
              join table2 in dt2.AsEnumerable()
              on table1.Field<string>("ColumnA") equals table2.Field<string>("ColumnC") 
              where (table1.Field<string>("ColumnB").Equals(table2.Field<string>("ColumnA"))) 
              || (table1.Field<int>("ColumnC") == table2.Field<int>("ColumnD")) 
              select table1;

UPDATE

If 'ds' is actually a datatable then do the following change:

var matched = from table1 in ds.AsEnumerable()

UPDATE 2

Based on your picture in the comments it looks like the mapping should be:

table1.ColumnA = table2.ColumnC - set this to string
table1.ColumnB = table2.ColumnA - set this to string
table1.ColumnC = table2.ColumnD - set this to int

It also looks like the types you cast to should be altered. I've updated the query with that in mind.

UPDATE 3

Following my previous comment try and strip out just the case numbers from both datatables and compare them. That will tell you whether the join should return any values. To do that you can use the following query:

var dsCaseNumbers = ds.AsEnumerable().Select(x => x.Field<string>("ColumnA").ToList();

This will return a list of just the case numbers in ds. Create another list using the same query for dt:

var dtCaseNumbers = dt.AsEnumerable().Select(x => x.Field<string>("ColumnC").ToList();

Then compare the 2 by doing:

var HasMatches = dsCaseNumbers.Any(y => dtCaseNumbers.Contains(y));

If there are matches between dsCaseNumbers and dtCaseNumbers 'HasMatches' will be true, if not then it will be false.

UPDATE 4

To display only the differences you can use the following:

var NoMatches = ds.AsEnumerable().Except(matched);

You can then use 'NoMatches' as the datasource for another datagrid. If the 'NoMatches' is determined by the table with the most rows and not always 'ds' then wrap the queries in a method that takes a 'main table' parameter and 'secondary table' parameter like this:

private IEnumerable<DataRow> NoMatches (DataTable MainTable, DataTable SecondaryTable)
{
    var matched = from table1 in MainTable.AsEnumerable()
                  join table2 in SecondaryTable.AsEnumerable()
                  on table1.Field<string>("ColumnA") equals table2.Field<string>("ColumnC") 
                  where (table1.Field<string>("ColumnB").Equals(table2.Field<string>("ColumnA"))) 
                  || (table1.Field<int>("ColumnC") == table2.Field<int>("ColumnD")) 
                  select table1;

    return MainTable.AsEnumerable().Except(matched);
}

Then simply do a count on the rows in each table first to determine which table should be the main table and call the method outlined above.

UPDATE 5

So, to use the method above and display non-matches in a new datagrid you can do the following:

//First determine which table should be considered 'Main'. This will be the one with the LEAST number of rows.
var dsRowCount = ds.AsEnumerable().Count();
var dtRowCount = dt.AsEnumerable().Count();

if (dsRowCOunt > dtRowCount)
{
    //Set main table to be dt as this has the least number of rows.
    datagridView.Datasource = NoMatches(dt, ds);
}
else
{
    //Set main table to ds as this has the least number of rows OR tables have the same number of rows.
    datagridView.Datasource = NoMatches(ds, dt);
}

UPDATE 6

Edit private IEnumerable NoMatches to return a DataTable instead like this:

private DataTable NoMatches (DataTable MainTable, DataTable SecondaryTable)
{
    var matched = from table1 in MainTable.AsEnumerable()
                  join table2 in SecondaryTable.AsEnumerable()
                  on table1.Field<string>("ColumnA") equals table2.Field<string>("ColumnC") 
                  where (table1.Field<string>("ColumnB").Equals(table2.Field<string>("ColumnA"))) 
                  || (table1.Field<int>("ColumnC") == table2.Field<int>("ColumnD")) 
                  select table1;

    return MainTable.AsEnumerable().Except(matched).CopyToDataTable();
}

Note the 2 changes; the method signature and the return using 'CopyToDataTable()'

sr28
  • 4,728
  • 5
  • 36
  • 67
  • @myworld - sorry, forgot select clause at the end. – sr28 Aug 11 '16 at 09:38
  • 'System.Data.DataTable' does not contain a definition for 'Tables' and no extension method 'Tables' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?) – myworld Aug 11 '16 at 09:43
  • @myworld - 'ds' is defined as a dataset in your code. Are you actually saying that 'ds' is a datatable? If so, just do ds.AsEnumerable() as you were before. This will test whether the changes in the where clause make a difference. – sr28 Aug 11 '16 at 09:43
  • yes ds is a datatable at the top of the code i set it as a Datataable – myworld Aug 11 '16 at 09:45
  • @myworld - sorry, I see that now. Can you tell me what happens if you make the changes as suggested in the comment above? – sr28 Aug 11 '16 at 09:46
  • Then i get this error : Cannot apply indexing with [] to an expression of type 'method group' – myworld Aug 11 '16 at 09:48
  • @myworld - have you amended the query as above (new update). – sr28 Aug 11 '16 at 10:09
  • I tried the new code still this error : 'System.Data.DataTable' does not contain a definition for 'Tables' and no extension method 'Tables' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?) – myworld Aug 11 '16 at 10:10
  • @myworld - try again, remove the '.Tables' (forgot to take it out). – sr28 Aug 11 '16 at 10:12
  • ok thanks the error is gone. How i should test it now. It is so okay? dataGridView3.DataSource = matched; – myworld Aug 11 '16 at 10:13
  • @myworld - try setting a breakpoint after var matched and debugging. You should then be able to hover over 'var matched' and drill down to see if any values are returned. – sr28 Aug 11 '16 at 10:15
  • ok i set a breakpoint no values returned only null :/ only ds and dt return the tables values – myworld Aug 11 '16 at 10:17
  • i think this code will not work for me because the two tables has not the exactly same sheme – myworld Aug 11 '16 at 10:19
  • @myworld - it shouldn't matter if they have different schema as you are telling it how to match them. – sr28 Aug 11 '16 at 10:23
  • how do you mean match them? – myworld Aug 11 '16 at 10:25
  • @myworld - I think your issue may lie with when it does the casting to various types. Can you check that when ds is created that all the columns actually match the data types you've given them e.g. ColumnA is actually set to a data type of int in ds. – sr28 Aug 11 '16 at 10:29
  • here i upload a Picture : Table 1, Table 2 and the last table the result so it should be. https://s10.postimg.org/ei63qgpex/Bild.png – myworld Aug 11 '16 at 10:30
  • @myworld - you need to double check that when you fill 'ds' that the data types are set up as you expect. At the moment you are expecting ColumnA to be of type int, but you need to double check that. It may well be setting that to string. – sr28 Aug 11 '16 at 10:32
  • the Problem is that the data and the colums are mixed please look at the Picture i upload. I hope you will understand the sheme – myworld Aug 11 '16 at 10:32
  • yes it is a string but the other column is also a string but dont match together. The first line should be Table A and Table C it is possible? – myworld Aug 11 '16 at 10:34
  • @myworld - when matching strings try using .Equals(the comparison value). I've modified the query above for ColumnC as that was comparing strings. – sr28 Aug 11 '16 at 10:36
  • ok but i have only one D Coulumn on Table 2 should i delete this code row ? – myworld Aug 11 '16 at 10:38
  • ok i have done what you said but it is still Returns 0 have you look at the Picture it ist really correct what we do now? – myworld Aug 11 '16 at 10:40
  • @myworld - if there is no ColumnD in table1 then remove that comparison in the where clause. – sr28 Aug 11 '16 at 10:42
  • @myworld - I've looked at your picture. Can you show how this data appears in 'ds' and 'dt2'? – sr28 Aug 11 '16 at 10:43
  • @myworld - ok, I think the mappings need to change. I've updated the answer with what I think the mappings should look like and the query as well. – sr28 Aug 11 '16 at 10:52
  • @myworld - just updated again. I think your data types need to change as well based on the new mapping. – sr28 Aug 11 '16 at 10:56
  • @myworld - ok, then just alter the comparison to cast to string and do .Equals(). I'll update the answer... – sr28 Aug 11 '16 at 11:01
  • but how i code update 2 replace it wit the other one? – myworld Aug 11 '16 at 11:01
  • @myworld - not sure I understand. Have you tried the latest query? – sr28 Aug 11 '16 at 11:08
  • ok now my code Looks so : var matched = from table1 in ds.AsEnumerable() join table2 in dt.AsEnumerable() on table1.Field("ColumnA") equals table2.Field("ColumnC") where (table1.Field("ColumnB").Equals (table2.Field("ColumnA")) || (table1.Field("ColumnC") == (table2.Field("ColumnD")))) select table1; – myworld Aug 11 '16 at 11:08
  • @myworld - That looks right, but only you can confirm the mappings are correct. – sr28 Aug 11 '16 at 11:10
  • yes the mapping is correct but the two string are still return null and int Returns that : MaxValue = 2147483647 MinValue = - 2147483648 – myworld Aug 11 '16 at 11:12
  • @myworld - Have you tried commenting out the where clauses and seeing if anything is returned if you just join on ds.ColumnA to dt.ColumnC? – sr28 Aug 11 '16 at 11:15
  • wehen i cmment the where clausel i still not get any value : Empty = "" – myworld Aug 11 '16 at 11:20
  • @myworld - so the join isn't working. Do the values have whitespace? Try ("ColumnA").Trim() and ("ColumnC").Trim(). – sr28 Aug 11 '16 at 11:22
  • No in the table are no whitespace, when i do that with trim i get the error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'. – myworld Aug 11 '16 at 11:25
  • @myworld - if both those columns are string you should be able to use Trim() perfectly fine. Can you give me some example data from table 1 that you expect to match table2? – sr28 Aug 11 '16 at 11:27
  • that are example data for Coulmn A Table 1 and C Table 2 : 1615119616 and 1615186613. I now noticed that i can wrote numbers and text on column a and c where i can see what datatyp it is ? – myworld Aug 11 '16 at 11:32
  • the Goal of the Programm is to Display the data that are missing in the small table and other difference in the same data – myworld Aug 11 '16 at 11:34
  • @myworld - to see the data type of a column mouse over the datatable, then click on 'Columns'. Go to 'Results View', then click on the column you want to look at. Under 'Data Type' it should tell you. – sr28 Aug 11 '16 at 11:37
  • @myworld - that can't be right in table1 as varchar is a database term. That would be string. – sr28 Aug 11 '16 at 11:40
  • @myworld - what type is dt.ColumnC? – sr28 Aug 11 '16 at 11:42
  • the table Comes form the SQL Server. I bound it in the datatable – myworld Aug 11 '16 at 11:42
  • one Moment i will look – myworld Aug 11 '16 at 11:44
  • @myworld - yes, in the database it will be of type varchar, but that gets converted to string in .net. If you follow my instructions you should be able to check the types for both columns in ds and dt. – sr28 Aug 11 '16 at 11:44
  • ok thanks but i Import the dt table from Excel where can i find in Excel the datatypes – myworld Aug 11 '16 at 11:46
  • @myworld - it's not the types in Excel or the DB that ultimately matter. It's the ones they get converted to in 'ds' and 'dt'. Each of those is a .NET DataTable, which contains columns which are marked as a certain data type. If you follow my instructions you can find out what they are. The reason you need to know is so you know whether you are actually comparing ints to strings etc. – sr28 Aug 11 '16 at 11:48
  • @myworld - when debugging set a breakpoint after when both dt and ds are filled. Then mouse over them, click on 'Columns'. Go to 'Results View', then click on the column you want to look at. Under 'Data Type' it should tell you. – sr28 Aug 11 '16 at 11:51
  • table 2: column 1 = string column 2 = string coloumn 3 = string and column 4 = double table 1 is under the way – myworld Aug 11 '16 at 11:57
  • @myworld - just popping out. Will take a look when I get back. – sr28 Aug 11 '16 at 11:59
  • table 1 column 1 = string column 2 = string column 3 = int16 column 4 = datetime – myworld Aug 11 '16 at 12:00
  • ok thank you I have to go in about 50 minutes. I would be glad when you can help me tommorow when you want. I have 8 hours time tommorow from 8 am to 3 pm. I would be happy when you can help at this time. – myworld Aug 11 '16 at 12:28
  • @myworld - Looks like you're comparing table1.ColumnA (string) with table2.ColumnC (string), so should work. So, are you certain you should actually be returning values? Perhaps null is the correct result? Can you see any examples that should be returned? – sr28 Aug 11 '16 at 12:48
  • I do not think that null correct it but there are a lot od datas i will try again – myworld Aug 11 '16 at 12:50
  • i can not say if null the correct answer is but i do not think so – myworld Aug 11 '16 at 12:52
  • but how can i Display the results in an another datagrid maybe then i could see any values – myworld Aug 11 '16 at 12:55
  • @myworld - well, if you go back to where you commented out the where clause and just try and do the join, that should pull back anything that matches between table1.ColumnA and table2.ColumnC. Looking at your data this looks like it might be telephone number or something like that? – sr28 Aug 11 '16 at 12:57
  • @myworld - to see the values just set a breakpoint after var matched and then mouse over 'matched' and see if there are values in it (Click on Results View). – sr28 Aug 11 '16 at 12:59
  • what should i do with the numbers? – myworld Aug 11 '16 at 12:59
  • Ok, so you really need to see if you can find a case number that appears in both tables then. Let's try and strip the values out from both tables first and just compare those. At least then we know whether this should be returning values or not. I'll add the new query to the answer... – sr28 Aug 11 '16 at 13:04
  • ok but the Goal of the program is to look if something from the other table is missing al value for example and when it is missing or different it should Displays it on the datagrid – myworld Aug 11 '16 at 13:08
  • ok i have looked and i have not found a case number that appears in both tables. – myworld Aug 11 '16 at 13:16
  • I have to go know i will test your query code that you will post tommorow. I hope then that it works when not i hope you can help me thanks – myworld Aug 11 '16 at 13:17
  • @myworld - ok, when you get a chance try my new query that will determine if should expect any matches between the case numbers. If when debugging this returns 'false' then you know there are no matching values. That would mean the var match query is potentially correct in that it's also not returning anything. – sr28 Aug 11 '16 at 13:19
  • ok when i set a breackpoint and debug the query code var HasMatches Returns false. ok what is the next step? as a reminder the program should finally Displays the differences and missing values that are not in the table 1. Thank you! – myworld Aug 12 '16 at 06:19
  • @myworld - so if it returns false then it means none of them are the same, so there will be no matches. That means all of the case numbers are different between the 2 datatables. – sr28 Aug 12 '16 at 08:20
  • ok but how i can Display the differences in an another datagridview – myworld Aug 12 '16 at 08:22
  • @myworld - looking at your existing code it looks like you only want to display the data from table 1 that doesn't have a match. Is that right? Or do you want to display all records from table 1 and 2 that don't match? – sr28 Aug 12 '16 at 08:26
  • table 1 is the small table and has 7000 rows and table 2 has about 3700 rows and i want to Display the data form table 1 that does not have a match with data from table 2 – myworld Aug 12 '16 at 08:29
  • @myworld - I'm assuming you missed off a '0' from the number of rows in table 2 as I checked your question and it said 37000. So, if that's the case you only want to display the records in table 1 that don't have a match. Is it likely that this may change to table 2 in the future? – sr28 Aug 12 '16 at 08:32
  • @myworld - forgot that the new method should return IEnumerable (which is what NoMatches will be). – sr28 Aug 12 '16 at 08:49
  • should i reaname dt and ds in var matched? But then how he can read the two datatables? – myworld Aug 12 '16 at 08:52
  • ok but datatable maintable and secondary table are empty? or should i write ds and dt? – myworld Aug 12 '16 at 08:59
  • and how i can Display the result datagridView.Datasource = ?; what should i wrote after datasource. This code will be in the burron click thank you – myworld Aug 12 '16 at 09:08
  • and column C from table 1 has int16 and column D from table 2 has double. in the code is int it is correct or should we convert? – myworld Aug 12 '16 at 09:35
  • @myworld - if you want 'NoMatches' to be displayed in a datagrid then you need to make the call to the NoMatches method the datasource e.g. datagridView.Datasource = NoMatches(ds, dt); – sr28 Aug 12 '16 at 09:48
  • @myworld - MainTable and SecondaryTable are just parameters that are then used within the NoMatches method. So when you call this method you input which datatable should be the main one. In this case it's ds. However, you should simply do a count on the rows before calling the method to determine which table should be MainTable. – sr28 Aug 12 '16 at 09:51
  • @myworld - with regards to converting to int that should be ok. I'd double check with some test data first though. So rather than filling ds and dt from the DB and excel, just put some test data in that has the same format. Make sure that some of the data should match so you can test matches are picked up and not included in the NoMatches data. – sr28 Aug 12 '16 at 09:56
  • i do not know if i understand right the maintable and secondeay table i should not delete? And in button click i should call ds and dt but there are not used? – myworld Aug 12 '16 at 10:00
  • i inserted update 5 in a button but when i click on the button nothing happen – myworld Aug 12 '16 at 10:19
  • @myworld - can you update your question with your new code please? – sr28 Aug 12 '16 at 10:20
  • @myworld - have you tried debugging? If so, do dsRowCount and dtRowCount have values? – sr28 Aug 12 '16 at 10:33
  • they have the values 0 – myworld Aug 12 '16 at 10:34
  • @myworld - When you click your button you need to ensure that you are also filling your 2 datatables (ds and dt). If you are doing this inside of another method then call that method first to ensure they are filled. – sr28 Aug 12 '16 at 10:39
  • they are filled up when i start the program i load at first ds and Display it and then dt but when i click at the result button nothing happens. – myworld Aug 12 '16 at 10:41
  • @myworld - looks like 'public void btn_Click(object sender, EventArgs e)' fills ds and dt is filled by public void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e). – sr28 Aug 12 '16 at 10:42
  • yes that is correct and btn_Click1 should fill result – myworld Aug 12 '16 at 10:44
  • @myworld - you should probably separate out filling of ds into it's own method that is then called by btn_Click. – sr28 Aug 12 '16 at 10:44
  • @myworld - don't you mean button1_Click? Are you sure your button is wired up correctly? – sr28 Aug 12 '16 at 10:45
  • yes i mean button 1_Click. I do not understand what do you mean : @myworld - you should probably separate out filling of ds into it's own method that is then called by btn_Click. – myworld Aug 12 '16 at 10:47
  • my button is alright i created another button to check it but it still not work so the button is not the problem – myworld Aug 12 '16 at 10:49
  • @myworld - at the moment, it looks like at the time you click your new button ds and dt are empty. When you debug, mouse over ds and dt at the top of the code and this should show this. You need to make sure that they are filled at the time of clicking your new button. 1 way of doing this would be to call the methods that fill them. – sr28 Aug 12 '16 at 10:50
  • yes i checked it dt and ds are filled up only dsRowCount and dtRowCount are 0 – myworld Aug 12 '16 at 10:53
  • @myworld - try changing them so they count the rows collection in the datatables like this dsRowCount = ds.Rows.Count; – sr28 Aug 12 '16 at 10:59
  • what should i Change? you mean dt and ds? – myworld Aug 12 '16 at 11:01
  • @myworld - no. At the moment you have 2 variables called dsRowCount and dtRowCount. Try amending them as per my comment above. You said they are both 0, so clearly the current way of count the rows in the datatables is not working. – sr28 Aug 12 '16 at 11:03
  • ok i changed it an they are still null dt and das are 100 % filled – myworld Aug 12 '16 at 11:05
  • he Count the rows but dsRowCount and dtRowCount are 0 – myworld Aug 12 '16 at 11:06
  • @myworld - so if you mouse over the 'ds' part of ds.Rows.Count, is it null? – sr28 Aug 12 '16 at 11:22
  • no if i over ds.Rows.Count it is 7469 but if i over dsRowCount it is 0 and ds is filled with the data – myworld Aug 12 '16 at 11:23
  • @myworld - make sure when debugging you set your breakpoint AFTER dsRowCount is being set. – sr28 Aug 12 '16 at 11:29
  • ok after i set the breakpoint after dsRowCount has the value 7469 – myworld Aug 12 '16 at 11:31
  • @myworld - so step through your new bit of code making sure that dsRowCount and dtRowCount have values. Then see which part of the 'if statement' it enters and whether it sets the datagrid source. I'm assuming the datagridView3 is an existing datagrid? – sr28 Aug 12 '16 at 11:34
  • yes dataGridView3 is existing. An the if Statements have the values of boths row Count (ds,dt). And dt and ds are filled with the table data – myworld Aug 12 '16 at 11:38
  • @myworld - ok, check that the new method NoMatches is returning values. Just before setting the datasource put in a new variable var NoMatchesCheck = NoMatches(ds, dt); and debug and see if NoMatchesCheck has any values. – sr28 Aug 12 '16 at 11:50
  • that is what NoMatchesCheck Show : NoMatchesCheck = {System.Linq.Enumerable.ExceptIterator} – myworld Aug 12 '16 at 11:55
  • @myworld - and what does it show in the Results View? – sr28 Aug 12 '16 at 13:09
  • visual studio base = { " Specified cast is not valid. "} – myworld Aug 12 '16 at 13:11
  • @myworld - one of the castings is wrong then. You may need to convert a field to something else using Convert.ToInt32 or something. – sr28 Aug 12 '16 at 13:20
  • i had to go in 5 minutes when you have a solution to this Problem pleas post it thanks – myworld Aug 12 '16 at 13:20
  • can it be the to columns table 1 Column C int16 and Table 2 Column D double? How should i convert them – myworld Aug 12 '16 at 13:21
  • @myworld - are they both whole numbers? If so do something like this Convert.ToInt32(table1.Field("ColumnC")) == Convert.ToInt32(table2.Field("ColumnD")) – sr28 Aug 12 '16 at 13:25
  • get this error : The type or namespace name 'int16' could not be found (are you missing a using directive or an assembly reference?) – myworld Aug 12 '16 at 13:29
  • @myworld - type Int16 – sr28 Aug 12 '16 at 13:32
  • ok now i test it and NoMatchesCheck Returns :{System.Linq.Enumerable.ExceptIterator} and under it : NoMatchesCheck, results Expanding the Results View will enumerate the IEnumerable. But there still no Data on the datagridview. Hope you can help – myworld Aug 16 '16 at 05:59
  • @myworld - can you show the code of your datagridview? Just update your question and add the code in a new section (not part of the existing code). – sr28 Aug 16 '16 at 07:50
  • I have not a datagridview code it is empty. I have only the code in the button from you who should Display the data. – myworld Aug 16 '16 at 07:53
  • @myworld - you need front end code for the datagridview or create it in code behind and add it to the controls. I'm guessing in your situation you will be better off adding front end code. Are you webforms, winforms, wpf? – sr28 Aug 16 '16 at 08:01
  • @myworld - ok, well on your form you will need to add a datagridview. Then set the datasource of that datagridview as per the code already in the answer. – sr28 Aug 16 '16 at 08:06
  • it is still not Display anything – myworld Aug 16 '16 at 08:28
  • @myworld - Have you added a datagridview to your form? If so then you don't need to add an 'CellContentClick' event. You just need to reference that datagridview when creating the datasource. Here is an example: https://msdn.microsoft.com/en-us/library/y0wfd4yz(v=vs.110).aspx. However, instead of creating a datagridview in the code behind you can just add it to your form. – sr28 Aug 16 '16 at 11:57
  • yes i have added it in the form and then made a double click on it. It is wrong? – myworld Aug 16 '16 at 11:59
  • @myworld - no, not wrong. That purely depends on how you want to get the data to populate. If you want it to show on a double click that's up to you. – sr28 Aug 16 '16 at 12:09
  • ok i referenced the datagridview but it is still not dissplay anything :/ Any other ideas? – myworld Aug 16 '16 at 12:12
  • @myworld - so based on your update I'm assuming you've added a datagridview to your form called 'dataGridView3'? At the moment it then looks like you've set it to add the data to the datasource when you click the content of the datagrid, which it can't as there's no content. If you want it to happen on a click of a button or something, create the button, double click it to create the click event and copy and paste your code in your update into that. – sr28 Aug 16 '16 at 12:16
  • the code was from the beginning in a button click Event but it is not working. I do not hacve an idea why not. – myworld Aug 16 '16 at 12:19
  • i updated the code so it Looks at the moment do you see anything that could be the cause of the Problem ? – myworld Aug 16 '16 at 12:25
  • @myworld - have you got AutoGenerateColumns set to true? – sr28 Aug 16 '16 at 14:08
  • AutoGenerateColumns was None. I set it true than false both are not working. – myworld Aug 17 '16 at 05:53
  • @myworld - can you take a screen shot of your form and your datagridview with its properties? – sr28 Aug 17 '16 at 07:48
  • @myworld - for some reason I can't see those. – sr28 Aug 17 '16 at 08:06
  • should i convert too? the last row int16 and double to int?7 – myworld Aug 17 '16 at 08:40
  • @myworld - yes, convert as you did before. – sr28 Aug 17 '16 at 08:41
  • @myworld - great. Sorry I didn't spot the issue with using IEnumerable with datagridviews sooner. – sr28 Aug 17 '16 at 08:45