Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DataGridView1 has list of software in Column Installed_Software, and 
DataGridView2 has a list of Installed_Software. I want DataGridView2 Installed_Software Column Compared to DataGridView1 Installed_Software Column,
and what Installed_Software DataGridView2 has that DataGridView1 does not have, I want Displayed in DataGridView3. If no Difference between the DGV1 and DGV2, then have MessageBox say something like Match.


What I have tried:

Below is the how the DataGridViews 1 and 2 or populated with the Columns Domain_User, Installed_Software:

dgv1 = DataGridView1 and dgv2 = DataGridView2 in the Code below.
  
 con = new SqlConnection();
            con.ConnectionString = @"Data Source=STACYWLENOVO; Initial Catalog=SoftwareHardwareTracking; Integrated Security=TRUE";

            con.Open();
            adp = new SqlDataAdapter("select Domain_User, Installed_Software from tblBkupSoftwareCount", con);
            ds1 = new System.Data.DataSet();
            adp.Fill(ds1, "Client Details");
            dgv2.DataSource = ds1.Tables[0];
            // https://stackoverflow.com/questions/8983277/how-to-convert-dataset-to-datatable
            //Code below to get dataset into DataTable dt2
            DataTable dt2 = ds1.Tables[0];
            con.Close();


            //Code pulling tblSoftwareCount
            conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=STACYWLENOVO; Initial Catalog=SoftwareHardwareTracking; Integrated Security=TRUE";

            conn.Open();
            adap = new SqlDataAdapter("select Domain_User, Installed_Software from tblSoftwareCount", conn);
            ds = new System.Data.DataSet();
            adap.Fill(ds, "Client Details");
            dgv1.DataSource = ds.Tables[0];
            //https://stackoverflow.com/questions/8983277/how-to-convert-dataset-to-datatable
            //Code below to get dataset into DataTable dt1
            DataTable dt1 = ds.Tables[0];
            conn.Close();


            dt3 = (dt1, dt2);

              if (dt.Rows.Count == 0)
                 
                    MessageBox.Show("Equal");
               else

              MessageBox.Show("Not Equal");
Posted
Updated 9-Dec-17 20:12pm
v3
Comments
Karthik_Mahalingam 8-Dec-17 22:38pm    
does the user has multiple software ?
Member 12457690 8-Dec-17 23:07pm    
DataGridView1 has list of software in Column Installed_Software, and
DataGridView2 has a list of Installed_Software. I want DataGridView2 Installed_Software Column Compared to DataGridView1 Installed_Software Column,
and what Installed_Software DataGridView2 has that DataGridView1 does not have, I want Displayed in DataGridView3. If no Difference between the DGV1 and DGV2, then have MessageBox say something like Match. I need the Values by name check, not the number of rows necessary. For exampl, DGV1 Installed_Software = MS Office 2010 and DGV2 Installed_Software = MS Office 2013, If DGV1 Dose not have it, then DGV3 needs to have it store to reflect the difference in Installed_Software between DGV1 and DGV2. Thank you. I'm really been trying to figure this out. Your help is appreciated.
Karthik_Mahalingam 10-Dec-17 2:12am    
check the solution

As you are using DataTable, you can try DataTable.AsEnumerable().Except()
See example here: DataTable Comparison Using LINQ Except, Intersect and Union : www.dotnetmentors.com[^]
 
Share this answer
 
Comments
Maciej Los 9-Dec-17 14:38pm    
5!The main thing is to compare data instead of its string representation.
try
DataTable dt1 =  // get from tblBkupSoftwareCount
DataTable dt2 =  // get from tblSoftwareCount

DataTable dtDifference = new DataTable();
dtDifference.Columns.Add("Domain_User");
dtDifference.Columns.Add("Installed_Software");

var list1 = dt1.AsEnumerable().Select(k => (new { user = k["Domain_User"].ToString(), sw = k["Installed_Software"].ToString() })).ToList();
var list2 = dt2.AsEnumerable().Select(k => (new { user = k["Domain_User"].ToString(), sw = k["Installed_Software"].ToString() })).ToList();
list1.AddRange(list2);


var diff = list1.GroupBy(k => new { Domain_User = k.user, Installed_Software = k.sw }).Where(k => k.Count() == 1).ToList();
diff.ForEach(k => { dtDifference.Rows.Add(k.Key.Domain_User, k.Key.Installed_Software); });
// dt3 has the difference
datagridview3.datasource = dtDifference;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900