私はプログラミングがうまくいかず、最近私はインターンシップで仕事をしています。その中で、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
です。
[こちら](http://stackoverflow.com/questions/27985535/vb-net-for-each-loop-that-compares-a-row-to-every-other-row)をご覧ください。それはすでにここで答えられています。 "UnitCost"または "FromDate"は、必要なセルのインデックスで置き換えることができます。 – Leprechaun
ありがとう@Leprechaun私はここでこの質問をする前にそのポストを読んだが、条件が真ならば色は変わるだけだが、私がやりたかったのは、最初のファイルのセル(0)を2番目のファイルセル(0)と同じ場合は、両方のファイルのcell(5)とcell(6)の値を減算し、別のdatagridviewの3番目の形式の名前で結果を表示します。 (6)は数字を含む。おかげで –