如何将2个datagridview与不同的值进行比较 [英] How can I compare 2 datagridview with different values

查看:110
本文介绍了如何将2个datagridview与不同的值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个datagridview。 dg1有3450个数据,而dg2有3250个数据。如果我点击btnFilter,不同的数据将显示到另一个datagridview。但现在一旦我点击按钮,索引超出范围。必须是非负的并且小于集合的大小。参数名称:索引已出现在行中如果DataGridView1.Rows(x).Cells( 1).Value.ToString = dgBranch.Rows(x).Cells(1).Value.ToString然后



我尝试过:



i已尝试此代码,但它返回索引超出范围错误



i have two datagridview. dg1 has 3450 data while dg2 has 3250 data.if i click the btnFilter, the different data will show up to another datagridview. but now once i click the button, "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" has appeared in the line If "DataGridView1.Rows(x).Cells(1).Value.ToString = dgBranch.Rows(x).Cells(1).Value.ToString Then"

What I have tried:

i have tried this code but it returns the index out of range error

Public Sub filter()
        Dim y As Integer = lblFilter.Text
        Form3.Label2.Text = Me.dgBranch.Rows(1).Cells(2).Value.ToString
        For x As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
            If DataGridView1.Rows(x).Cells(1).Value.ToString = dgBranch.Rows(x).Cells(1).Value.ToString Then
                If DataGridView1.Rows(x).Cells(y).Value.ToString <> dgBranch.Rows(x).Cells(y).Value.ToString Then
                    'MessageBox.Show(DataGridView1.Rows(x).Cells(1).Value.ToString + " " + dgBranch.Rows(x).Cells(1).Value.ToString + " " + DataGridView1.Rows(x).Cells(5).Value.ToString + " " + dgBranch.Rows(x).Cells(5).Value.ToString)
                    Form3.Show()
                    Form3.dgServer.Rows.Add(DataGridView1.Rows(x).Cells(0).Value, DataGridView1.Rows(x).Cells(1).Value.ToString, DataGridView1.Rows(x).Cells(3).Value.ToString, DataGridView1.Rows(x).Cells(4).Value.ToString, DataGridView1.Rows(x).Cells(5).Value.ToString)
                    Form3.dgBranch.Rows.Add(dgBranch.Rows(x).Cells(0).Value, dgBranch.Rows(x).Cells(1).Value.ToString, dgBranch.Rows(x).Cells(3).Value.ToString, dgBranch.Rows(x).Cells(4).Value.ToString, dgBranch.Rows(x).Cells(5).Value.ToString)
                End If
            Else
                MessageBox.Show("All Selected Items are the same!", "Filter", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Next
    End Sub<pre>

推荐答案

如果DGV的记录数量不同然后你不能使用较大的DGV的索引对较小的。



dg1可以从3450行向后计数,dg2(dgbranch?)没有行号因此,在3249之后,指数3449超出了该DGV的范围。



为了给出完整的答案,我需要了解两个DGV之间的关系。您如何配对它们但是至少应该在使用之前测试索引是否在第二个DGV的范围内,或者使用具有最低行数的DGV作为for循环控制。
If the DGV's have different numbers of records then you cannot use the index of the larger DGV against the smaller.

dg1 can count backwards from 3450 rows, dg2 (dgbranch?) does not have a row number after 3249 therefore the index 3449 is out of range for that DGV.

In order to give a complete answer I would need to understand understand the relationship between the two DGV's and how you are pairing them but as a minimum you should test the index is in range of the second DGV before you use it or use the DGV with the lowest row count as the for loop control.


这篇关于如何将2个datagridview与不同的值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆