简体   繁体   English

如何比较Vb.net中的一个datagridview所有行索引与另一datagridview所有行索引?

[英]how to compare one datagridview all rows index to other datagridview all rows index in Vb.net?

I'm not good at programming and I recently got a task to do at my internship in which I have to compare one Excel File with another Excel file using Vb.net. 我不太擅长编程,最近我在实习时要完成一项任务,我必须使用Vb.net将一个Excel文件与另一个Excel文件进​​行比较。 I've made the form one in which I can select two different Excel files Image here. 我已经制作了一种表单,可以在其中选择两个不同的Excel文件 By clicking the view data button a new form is open to show data in data grid view same case with second file Image Here . 通过单击查看数据按钮,将打开一个新表单,以在数据网格视图中显示数据,并与第二个文件Image Here相同 All I want to do now is compare cell(0)(5)(6) of all rows only from datagridview of first file with the same cells of all rows of datagridview of the second file and show the difference of cell(5) and cell(6) in a new datagridview on another form. 我现在要做的就是仅比较第一个文件的datagridview中所有行的cell(0)(5)(6)和第二个文件的datagridview中所有行的相同单元格,并显示cell(5)和另一种形式的新datagridview中的cell(6)。 I know that nested for loop will be required for that which will check the first row cell(0) with the second file first row cell(0) and if they are same then it will give difference of the cell(5)(6) in the result form if not then it will check the first row cell(0) of first file with the second row cell(0) with the second file. 我知道将需要嵌套的for循环,它将检查第一行cell(0)和第二个文件的第一行cell(0),如果它们相同,则将给出cell(5)(6)的区别如果结果不是,则将检查第一个文件的第一行单元格(0)和第二个文件的第二行单元格(0)。 Please help me with this code because I'm not good with loops. 请帮助我使用此代码,因为我对循环不好。 Here is the code 这是代码

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb
Public Class Start_Up
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Dim nTemp As Integer
    Dim sMsg As String
    sMsg = "Are you sure you want to exit the program?"
    nTemp = MessageBox.Show(sMsg, "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If nTemp = DialogResult.Yes Then
        Application.Exit()
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    File_1.Show()
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    File_2.Show()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim conn As OleDbConnection
    Dim dta As OleDbDataAdapter
    Dim dts As DataSet
    Dim excel As String
    Dim openfiledialog As New OpenFileDialog

    openfiledialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    openfiledialog.Filter = "All Files (*.*)|*.*|Excel Files (*.xlsx)|*.xlsx| CSV Files (*.csv)|*.csv| XLS Files (*.xls)|*xls)"

    If (openfiledialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        Dim fi As New FileInfo(openfiledialog.FileName)
        Dim filename As String = openfiledialog.FileName
        TextBox1.Text = fi.ToString
        excel = fi.FullName
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
        dta = New OleDbDataAdapter("Select * From[ITR$]", conn)
        dts = New DataSet
        dta.Fill(dts, "[ITR$]")
        File_1.DataGridView1.DataSource = dts
        File_1.DataGridView1.DataMember = "[ITR$]"

        conn.Close()
        If TextBox1.Text = fi.ToString Then
            CheckBox1.Checked = True
        End If
    End If

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim conn As OleDbConnection
    Dim dta As OleDbDataAdapter
    Dim dts As DataSet
    Dim excel As String
    Dim openfiledialog As New OpenFileDialog

    openfiledialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    openfiledialog.Filter = "All Files (*.*)|*.*|Excel Files (*.xlsx)|*.xlsx| CSV Files (*.csv)|*.csv| XLS Files (*.xls)|*xls)"

    If (openfiledialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        Dim fi As New FileInfo(openfiledialog.FileName)
        Dim filename As String = openfiledialog.FileName
        TextBox2.Text = fi.ToString
        excel = fi.FullName
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
        dta = New OleDbDataAdapter("Select * From[ITR$]", conn)
        dts = New DataSet
        dta.Fill(dts, "[ITR$]")
        File_2.DataGridView1.DataSource = dts
        File_2.DataGridView1.DataMember = "[ITR$]"

        conn.Close()
        If TextBox2.Text = fi.ToString Then
            CheckBox2.Checked = True
        End If
    End If
End Sub
Public Sub CompButton_Click(sender As Object, e As EventArgs) Handles   CompButton.Click
    'For progressbar on click of compare button (Starts)
    ToolStripProgressBar1.Value = 0
    Timer1.Start()
    CompButton.Enabled = False
    'For ProgressBar(End)
    If File_1.DataGridView1.RowCount() - 1 = File_2.DataGridView1.RowCount() - 1 Then
        MessageBox.Show("No New Entries Have Been Done or Removed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        MessageBox.Show("Some New Entries Have Been Done or Removed", "New Entries", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    End If
   '(THE LOOP WILL BE HERE)
    End Sub

Please help me with this code thanks. 请用此代码帮助我,谢谢。

For Each rowDGV1 As DataGridViewRow In DataGridView1.Rows

    For Each rowDGV2 As DataGridViewRow In DataGridView2.Rows

        If (rowDGV1.Cells(0).Value = rowDGV2.Cells(0).Value) And 
           (rowDGV1.Cells(5).Value = rowDGV2.Cells(5).Value) And
           (rowDGV1.Cells(6).Value = rowDGV2.Cells(6).Value) Then _
            AddRow(rowDGV1.Cells(0).Value, _
                   rowDGV1.Cells(5).Value, _
                   rowDGV1.Cells(6).Value)

    Next

Next


Private Sub AddRow(ByVal strName as String, ByVal strOne as String, ByVal strTwo as String)
    'Add a row into the 3rd dgv with the values presuming you have already added columns in it (for name and difference)

    Dim strValue as String = Cdbl(strOne) - Cdbl(strTwo)
    dgv3.Rows.Add()
    dgv3.Item(dgcName, dgv3.RowCount - 1).Value = strName
    dgv3.Item(dgcDifference,dgv3.RowCount - 1) = strValue
    dgv3.Item(dgcDGV1Col5,dgv3.RowCount - 1) = strOne
    dgv3.Item(dgcDGV1Col6,dgv3.RowCount - 1) = strTwo
    dgv3.Item(dgcDGV2Col5,dgv3.RowCount - 1) = strOne
    dgv3.Item(dgcDGV2Col6,dgv3.RowCount - 1) = strOne

End Sub

May be this can work. 也许这可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM