我正在尝试使用vb.net代码在datagridview中添加项目。我正在使用Excel数据库。这是我的代码,我尝试过的所有内容都以注释的形式出现,如果有人打电话帮助我,我将不胜感激。我整个星期都在尝试。谢谢
Imports System.Data.OleDb
Imports System.IO
Imports System.Data.DataTable
Imports microsoft.Office.Interop
Public Class Welcome
Private Sub ImportExceldata(DataExcelFile As String,ImportData As DataGridView)
If ComboBox1.SelectedIndex.Equals(0) Then
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbdataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbdataAdapter("SELECT * FROM [CandidatesCallCenterLocation$]",MyConnection)
'MyCommand.TableMappings.Add("Table","Attendace")
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
ImportData.DataSource = dataSet.Tables(0)
DataGridView.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
ElseIf ComboBox1.SelectedIndex.Equals(1) Then
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbdataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbdataAdapter("SELECT * FROM [ExpectedNumberofCalls$]","Attendace")
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
ImportData.DataSource = dataSet.Tables(0)
DataGridView.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
ElseIf ComboBox1.SelectedIndex.Equals(2) Then
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbdataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbdataAdapter("SELECT * FROM [CostProcessingTelephoneCalls$]","Attendace")
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
ImportData.DataSource = dataSet.Tables(0)
DataGridView.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
End Sub
Private Sub Button1_Click(sender As Object,e As EventArgs)
End Sub
Private Sub ComboBox_SelectedIndexChanged(sender As Object,e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ImportExceldata("C:\Users\Home\Desktop\dss.xlsx",DataGridView)
End Sub
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click
For Each row As DataGridViewRow In DataGridView.SelectedRows
DataGridView.Rows.Remove(row)
Next
End Sub
这部分由添加按钮和我尝试过的所有代码组成。我以为底部起作用了,但显然没有。
Private Sub Button1_Click_1(sender As Object,e As EventArgs) Handles Button1.Click
' Dim Table As New DataTable("Table")
' Table.Columns.Add("Area Code",Type.GetType("System.Int64"))
' Table.Columns.Add("Metropolitan Area",Type.GetType("System.Int64"))
'Table.Columns.Add("Annual Fixed Cost",Type.GetType("System.Int64"))
'Table.Columns.Add("Wage per hour",Type.GetType("System.Int64"))
'Table.Rows.Add("","","")
'Table.Rows.Add(2,"aaaa","bbbb",33)
'Table.Rows.Add(3,"cccc","dddd",53)
'Table.Rows.Add(4,"eeee","ffff",63)
'Table.Rows.Add(5,"rrrr","qqqq",73)
'Table.Rows.Add(6,"gggg","hhhh",83)
' Dim Add As Object
' Add = CreateObject("Excel.Application")
' Dim oneadd As Excel.Workbook
' Dim sheetadd As Excel.Worksheet
'Dim fexcel As String = "C:\Users\Home\Desktop\dss.xlsx"
' Dim curLine As Integer = 0
'oneadd = Add.Workbooks.Add
'If Add.Application.Sheets.Count() < 1 Then
'sheetadd = CType(oneadd.Worksheets.Add,Excel.Worksheet)
' Else
'sheetadd = Add.Worksheets(1)
'End If
'sheetadd.Name = "CandidatesCallCenterLocation$"
'sheetadd.Range("A1").Value = "Row1"
'oneadd.SaveAs(fexcel)
'oneadd.Close()
'oneadd = Nothing
'DataGridView.AllowUserToAddRows = False
'Me.DataGridView.Rows.Add(actextBox.Text,MATextBox.Text,AFCTextBox.Text,TextBoxWPH.Text)
End Sub
' Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click
'Dim cn As New OleDbConnection
' Dim cm As New OleDbCommand
'cn = New OleDbConnection("Provider=microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Home\Desktop\dss.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""")
'cn.Open()
'With cm
'.Connection = cn
'.CommandText = "update [up$] set [name]=?,[QC_status]=?,[reason]=?,[date]=? WHERE [article_no]=?"
'cm = New OleDbCommand(.CommandText,cn)
'cm.Parameters.AddWithValue("?",actextBox.Text)
' cm.Parameters.AddWithValue("?",ComboBox1.SelectedItem)
'cm.Parameters.AddWithValue("?",TextBox3.Text)
'cm.Parameters.AddWithValue("?",DateTimePicker1.Text)
'cm.Parameters.AddWithValue("?",TextBox1.Text)
'cm.ExecuteNonQuery()
'MsgBox("UPDATE SUCCESSFUL")
' MyConnection.Close()
'End With
'End Sub
End Class