2016-07-28 5 views
0

私はプログラミングがうまくいかず、最近私はインターンシップで仕事をしています。その中で、1つのExcelファイルとVb.netを使って別のExcelファイルを比較する必要があります。私は2つの異なるExcelファイルを選択することができるフォームを作成しましたImage here.ビューデータボタンをクリックすると、新しいフォームが開き、2番目のファイルImage Hereと同じケースのデータグリッドビューにデータが表示されます。私が今したいのは、2番目のファイルのdatagridviewのすべての行の同じセルを持つ最初のファイルのdatagridviewからのすべての行のcell(0)(5)(6)を比較して、cell cell(6)を別のフォームの新しいdatagridviewに追加します。 2番目のファイルの最初の行のセル(0)で最初の行のセル(0)をチェックし、同じ場合はセルの差異(5)(6 )が結果フォームにない場合は、2番目の行セル(0)を持つ最初のファイルの最初の行セル(0)と2番目のファイルをチェックします。私はこのコードで私を助けてください。なぜなら、私はループにうまく対応していないからです。ここ は、このコードのおかげで私を助けてくださいコードどのように1つのdatagridviewすべての行のインデックスを比較する他のdatagridviewすべての行のインデックスVb.netで?

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 

です。

+0

[こちら](http://stackoverflow.com/questions/27985535/vb-net-for-each-loop-that-c​​ompares-a-row-to-every-other-row)をご覧ください。それはすでにここで答えられています。 "UnitCost"または "FromDate"は、必要なセルのインデックスで置き換えることができます。 – Leprechaun

+0

ありがとう@Leprechaun私はここでこの質問をする前にそのポストを読んだが、条件が真ならば色は変わるだけだが、私がやりたかったのは、最初のファイルのセル(0)を2番目のファイルセル(0)と同じ場合は、両方のファイルのcell(5)とcell(6)の値を減算し、別のdatagridviewの3番目の形式の名前で結果を表示します。 (6)は数字を含む。おかげで –

答えて

0
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 

が、これは動作することができますように。

+0

ありがとう@Leprechaunこれは私を少し助けてくれましたが、プログラムのいくつかの変更のために、私はあなたがコードで私を助けることができる場合、私は最後の一箇所で立ち往生しています。私はすべて大丈夫です私は2つの列を比較することができるモジュールを作ったが、今私がしたいのは、DGV1列(0)DGV1 col(5)と(6)とDGV2 col 5)と(6)の間にある。 –

+0

私はコードを編集しましたが、カラム5と6は同じですので、基本的には情報を繰り返しています。これは本当にあなたが望むものですか? – Leprechaun

+0

@leprechaunありがとう@leprechaun私はそれを完了するか、私はあなたに知らせる助けを必要とするとすぐにランタイムエラーが発生しているので、コードは正常ですが、まだいくつかの作業が必要です。 –

関連する問題