Kaswin Singh
Kaswin Singh

Reputation: 1

Comparing two datagridviews

I have two datagridviews in one form. The first, datagridview1 has columns and data:

name   IC              EMAIL             TELEPHONE  
------------------------------------------------------
rOO    898989096677    [email protected]      018-9097878

datagridview2 has

name   IC              EMAIL           TELEPHONE      ID 
-----------------------------------------------------------
rOO    898989096677    [email protected]    018-9097878    8787

I would like to ask help on how to compare two datagridviews, as you can see in the figure I would like to compare the four columns from one datagridview to another datagridview and see if any results match. For example, does the roo name match with the another datagridview, I want the value in the id (8787) to be sent to another datagridview.

Upvotes: 0

Views: 4680

Answers (3)

Ryan Dooley
Ryan Dooley

Reputation: 254

Steve's answer does not work correctly and will not compile.

Here is my solution:

        int x = 0;
        int y = 0;
        int i = -1;
        int z = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                    i++;

                if ((dataGridView1.Rows[i].Cells[i].Value) == (dataGridView2.Rows[z].Cells[i].Value))
                {
                    x++;
                }
                else
                {
                    y++;
                }
            if (z < dataGridView2.Rows.Count)
            {
                z++;
            }
            if(z == dataGridView2.Rows.Count)
            {
                z--; //subtract 1 from the total count because the datagrid is 0 index based.
            }

     MessageBox.Show("Matched: " + x.ToString() + "\r\n" + "Not Matched: " + y.ToString());

A foreach loop on the datagrid will cycle through each row, you can then select a cell or even cells to compare. This code is a bit lengthy and I'm sure it can be simplified, however it does the job.

Currently this will check every row from datagrid 2 for a match in datagrid 2, only on a single column.

Its import to remember the datagrid is 0 index based. The Count property of the datagrid gives a [1]index based count, so we need to subtract the last iteration on datagrid 2.

Upvotes: 0

HTU
HTU

Reputation: 1034

there is a very simple solution to compare twoo datagridview and show their result in 3rd one.

        for (int i = 0; i < dtView1.Rows.Count; i++)
        {
            for (int j = 1; j < dtView1.Columns.Count; j++)
            {

                if ((dtView1.Rows[i][j]) == (dtView2.Rows[i][j]))
                {
                   // here you can add your own logic
                }
                else
                {
              // here you can add your own logic
            }
        }

|

Upvotes: 1

Alexandru Dicu
Alexandru Dicu

Reputation: 1235

Let's assume that you populate both datagrids with the same objects that contain these 4 properties from your example. You must check for the selected row from the first datagrid if in the second datagrid there is an matching object, based on your filters. Use Linq. If so, then copy the data from the selected item from the first datagrid into the matching element from the seconds. I am affraid that you will need to do all these steps manually, because there is no method that can compare two data grids and you just set some filters and the job is done. You will have some work to do, but it is not hard. Good luck.

Upvotes: 0

Related Questions