locked
How to compare two datagridviewrow are equal RRS feed

  • Question

  • I have two DataRow  dr1 and dr2 having same column

    Need to identify if these two rows are equal.After  excluding the row index column value.

    Because after the datagrid is updated the row index gets changed

    There are more than 10 columns so if possible a linq will be better

    Thanks in advance!

    • Moved by CoolDadTx Friday, May 3, 2019 1:39 PM Winforms related
    Friday, May 3, 2019 10:33 AM

Answers

  • Hello,

    Identify the two rows then use code similar to this where the Skip is skipping in this case a primary key field, you may need to adjust to exclude one or more fields but the core is to use SequenceEquals to compare.

    You will also need to of course change the DataGridView name and the index of the row.

    var Row1 = ((DataTable)customersSqlServerDataGridView.DataSource).Rows[0].ItemArray.Skip(1);
    var Row2 = ((DataTable)customersSqlServerDataGridView.DataSource).Rows[1].ItemArray.Skip(1);
    if (Row1.SequenceEqual(Row2))
    {
    	//
    }
    else
    {
    	//
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 3, 2019 11:39 AM

All replies

  • Hello,

    Identify the two rows then use code similar to this where the Skip is skipping in this case a primary key field, you may need to adjust to exclude one or more fields but the core is to use SequenceEquals to compare.

    You will also need to of course change the DataGridView name and the index of the row.

    var Row1 = ((DataTable)customersSqlServerDataGridView.DataSource).Rows[0].ItemArray.Skip(1);
    var Row2 = ((DataTable)customersSqlServerDataGridView.DataSource).Rows[1].ItemArray.Skip(1);
    if (Row1.SequenceEqual(Row2))
    {
    	//
    }
    else
    {
    	//
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 3, 2019 11:39 AM
  • Hi Joy123456,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 14, 2019 2:15 AM