Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to compare two datagridviews to see if they have the same data(barcode) and if they do delete it from the first datagridview.

What I have tried:

private void button7_Click(object sender, EventArgs e)
{
    for (int i = 0; i < RemakesGv.Rows.Count; i++)
    {
        var row1 = RemakesGv.Rows[i].Cells;
        var row2 = RzrBack2Gv.Rows[i].Cells;

        for (int j = 0; j < row1.Count; j++)
        {
            if (!row1[j].ToString().Equals(row2[j].Value.ToString()))
            {

                RzrBack2Gv.Rows[i].Cells.RemoveAt(j);
            }
        }
    }
Posted
Updated 29-Apr-22 4:37am
v2
Comments
[no name] 27-Apr-22 11:41am    
You need a "reject event handler"; and only you know the how, when, what, where and why that triggers it.
Member 14804208 2-May-22 9:40am    
How would that look?
Richard MacCutchan 29-Apr-22 7:52am    
What is the question?
Member 14804208 2-May-22 9:38am    
It doesnt work. I was looking for a working example of how to do this. I am new to programming. Thanks
Richard MacCutchan 2-May-22 10:08am    
What doesn't work? Please ensure that you provide full details of your problem.

1 solution

When deleting from collections by index, don't start at the front and work through to the end.
Start at the end and work backwards to the front.

Imagine you have 4 rows of data
Row # Data
1     Data Row 1
2     Data Row 2
3     Data Row 3
4     Data Row 4
Say you want to delete rows number 4 and 1. Start at the top and delete the first row and see what happens
Row # Data
1     Data Row 2
2     Data Row 3
3     Data Row 4
I.e. what was row number 2 is now row number 1 ... and there is no longer a row number 4

If you do it the other way around ... start at the end and delete row number 4
Row # Data
1     Data Row 1
2     Data Row 2
3     Data Row 3
Rows 1,2 and 3 "stay" where they were and can still be referenced by their original row numbers

EDIT after OP comment
Instead of using a loop counter that looks like this
C#
for (int j = 0; j < row1.Count; j++)
you would use a loop that looks more like this (warning - untested code)
C#
for (int j = row1.Count - 1; j >=0; j--)
Things to note:
1. The "start" of the loop is the number of rows minus 1 - because the row counter starts at zero.
2. The loop termination (j >= 0 must include 0
3. instead of counting j upwards (j++) you decrement j (count down) with j==
 
Share this answer
 
v2
Comments
Member 14804208 2-May-22 9:39am    
Thanks. How do I make it read from bottom up? I am new to programming
CHill60 3-May-22 4:02am    
I've added some information to my solution
Member 14804208 13-May-22 8:25am    
Thank you
CHill60 13-May-22 8:37am    
Beware - I've just noticed it says
j==
for the decrement, that should be
j--

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